vue mixins & extends

本文探讨了 Vue.js 中 Mixin 的使用技巧,包括全局 Mixin 和局部 Mixin 的执行顺序,以及局部 Mixin 如何覆盖全局 Mixin 的方法。通过实例展示了不同 Mixin 之间的交互方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

const myMixin = {
created(){
console.log(this.msg + '1')
},
methods:{
hello(){
console.log(this.msg + '2')
}
}
};
Vue.mixin({
created(){
console.log(this.msg + '3');
},
methods:{
hello(){
console.log(this.msg + '4');
}
}
})
//-----------------------------------------------------------
//如果全局mixin和组件内mixin有同名的钩子函数,钩子函数都会执行,并且先执行全局后执行组件内。
//hello3 index.html:30
//hello1 index.html:20
//-----------------------------------------------------------
Vue.filter('uppercase',function(value){
return value.toUpperCase();
})
var vm = new Vue({
el: "#app",
mixins:[myMixin],
data:{
msg:'hello'
}
});

2.

const myMixin = {
created(){
this.hello();
},
methods:{
hello(){
console.log(this.msg + '2')
}
}
};
Vue.mixin({
created(){
this.hello();
},
methods:{
hello(){
console.log(this.msg + '4');
}
}
})
//-----------------------------------------------------------
// 组件内的会覆盖全局的。
//hello2 index.html:24
//hello2 index.html:24
//-----------------------------------------------------------

3.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值