1👍
extends
and mixins
merge options in a specified way, this is the official way to inherit component functionality. They don’t provide full control over the inheritance.
For static data that may change between component definitions (Base
and Extended
) custom options are commonly used:
export default {
myName: 'Component',
data() {
return { name: this.$options.myName };
}
}
and
export default {
extends: Base,
myName: `Extended ${Base.options.myName}`
}
Notice that name
is existing Vue option and shouldn’t be used for arbitrary values.
Any reusable functions can be extracted from component definition to separate exports.
For existing components that cannot be modified data
and other base component properties can be accessed where possible:
data() {
let name;
name = this.$data.name;
// or
name = Base.options.data.call(this).name;
return { name: `Extended ${name}` };
}
This approach is not future-proof and not compatible with composition API, as there’s no clear distinction between data
, etc, and this
is unavailable in setup function. An instance can be accessed with getCurrentInstance
but it’s internal API and can change without notice.
- [Vuejs]-Vue dynamic import is being imported even if it is never used
- [Vuejs]-How to make a text box which shows inverts a input
0👍
The data object is a kind of state for a single component, hence the data object inside a component can only be accessed in that component and not from outside or other components.
For your specific requirement, you can use Vuex. It helps you access the state in multiple components. Simply define a state in the vuex store and then access the state in the component.
If you don’t want to use Vuex, then another simple solution is to store the data in localStorage and then access the data in multiple components.
- [Vuejs]-It is inserted using existing data. Why is a new row created?
- [Vuejs]-Chart.js how to configure the lable for both axis
-1👍
You can access your data, from child component. You can do that by this.$parent.nameOfTheDataYouHave.
This cheatsheet can help you with the basics of the component anatomy and lifecycle hooks(mounted,created,etc..).
And last this is the proper way of two-way binding data (parent-child).