我无法从组件调用 mixin 方法,出现此错误this.hello is not a function
.
我可以打电话hello()
来自 Vue 实例,但我无法在组件内调用它。
有什么事吗?!
<div id='vue-app'>
<cmp></cmp>
</div>
const mixin = {
methods: {
hello() {
return 'Hello World!';
}
},
created() {
console.log('Mixin Created!');
},
};
const cmp = {
created() {
console.log('From Cmp:', this.hello());
},
};
new Vue({
components: {
cmp
},
el: '#vue-app',
mixins: [mixin],
created() {
console.log('From VM:', this.hello());
},
});
https://jsfiddle.net/ar464soq/ https://jsfiddle.net/ar464soq/
正确的是,mixin 方法/数据仅在其添加到的实例中可用。但是,如果您确实想要在根实例中使用 mixin,您可以调用this.$root.hello()
来自根实例的任何子组件
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)