Vue mixin功能使用

mixin

mixin就是混入的意思,特别适合全局共享变量
同时在混入时Vue会自动合并名称相同的变量,但是会以当前页面定义的变量优先

var mixin = {
  data: function () {
    return {
      message: 'hello',
      foo: 'abc'
    }
  }
}

new Vue({
  mixins: [mixin],
  data: function () {
    return {
      message: 'goodbye',
      bar: 'def'
    }
  },
  created: function () {
    console.log(this.$data)
    // => { message: "goodbye", foo: "abc", bar: "def" }
  }
})
原文地址:https://www.cnblogs.com/bcde/p/14517353.html