可以专门搞一个js文件定义混入对象
export const mixinExample(){
var mixin = {
data: function () {
return {
message: 'hello',
foo: 'abc'
}
},
created: function () {
console.log('混入对象的钩子被调用')
}
}
}
import {mixinExample} from ' ';
new Vue({
mixins: [mixin],
data: function () {
return {
message: 'goodbye',
bar: 'def'
},
created: function () {
console.log('组件钩子被调用')
}
},
created: function () {
console.log(this.$data)
// => { message: "goodbye", foo: "abc", bar: "def" }
}
})
// => "混入对象的钩子被调用"
// => "组件钩子被调用"
网友评论