2020-05-01 vuejs 混入mixin

2020-05-01  本文已影响0人  sll_

可以专门搞一个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" }
  }
})
// => "混入对象的钩子被调用"
// => "组件钩子被调用"
上一篇 下一篇

猜你喜欢

热点阅读