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.