基础

混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被“混合”进入该组件本身的选项。

  1. // define a mixin object
  2. var myMixin = {
  3. created: function () {
  4. this.hello()
  5. },
  6. methods: {
  7. hello: function () {
  8. console.log('hello from mixin!')
  9. }
  10. }
  11. }
  12. // define a component that uses this mixin
  13. var Component = Vue.extend({
  14. mixins: [myMixin]
  15. })
  16. var component = new Component() // => "hello from mixin!"

选项合并

当组件和混入对象含有同名选项时,这些选项将以恰当的方式进行“合并”。 比如,数据对象在内部会进行递归合并,并在发生冲突时以组件数据优先。

  1. var mixin = {
  2. data: function () {
  3. return {
  4. message: 'hello',
  5. foo: 'abc'
  6. }
  7. }
  8. }
  9. new Vue({
  10. mixins: [mixin],
  11. data: function () {
  12. return {
  13. message: 'goodbye',
  14. bar: 'def'
  15. }
  16. },
  17. created: function () {
  18. console.log(this.$data)
  19. // => { message: "goodbye", foo: "abc", bar: "def" }
  20. }
  21. })

同名钩子函数将合并为一个数组,因此都将被调用。另外,混入对象的钩子将在组件自身钩子之前调用。

  1. var mixin = {
  2. created: function () {
  3. console.log('混入对象的钩子被调用')
  4. }
  5. }
  6. new Vue({
  7. mixins: [mixin],
  8. created: function () {
  9. console.log('组件钩子被调用')
  10. }
  11. })
  12. // => "混入对象的钩子被调用"
  13. // => "组件钩子被调用"

值为对象的选项,例如 methods、components 和 directives,将被合并为同一个对象。两个对象键名冲突时,取组件对象的键值对

  1. var mixin = {
  2. methods: {
  3. foo: function () {
  4. console.log('foo')
  5. },
  6. conflicting: function () {
  7. console.log('from mixin')
  8. }
  9. }
  10. }
  11. var vm = new Vue({
  12. mixins: [mixin],
  13. methods: {
  14. bar: function () {
  15. console.log('bar')
  16. },
  17. conflicting: function () {
  18. console.log('from self')
  19. }
  20. }
  21. })
  22. vm.foo() // => "foo"
  23. vm.bar() // => "bar"
  24. vm.conflicting() // => "from self"

注意:Vue.extend() 也使用同样的策略进行合并。

Global Mixin(全局混入) 混入也可以进行全局注册。使用时格外小心!一旦使用全局混入,它将影响 每一个 之后创建的 Vue 实例。使用恰当时,这可以用来为自定义选项注入处理逻辑

  1. // 为自定义的选项 'myOption' 注入一个处理器。
  2. Vue.mixin({
  3. created: function () {
  4. var myOption = this.$options.myOption
  5. if (myOption) {
  6. console.log(myOption)
  7. }
  8. }
  9. })
  10. new Vue({
  11. myOption: 'hello!'
  12. })
  13. // => "hello!"

请谨慎使用全局混入,因为它会影响每个单独创建的 Vue 实例 (包括第三方组件)。大多数情况下,只应当应用于自定义选项,就像上面示例一样。推荐将其作为插件发布,以避免重复应用混入。

自定义选项合并策略

自定义选项将使用默认策略,即简单地覆盖已有值。如果想让自定义选项以自定义逻辑合并,可以向Vue.config.optionMergeStrategies添加一个函数:

  1. Vue.config.optionMergeStrategies.myOption = function (toVal, fromVal) {
  2. // 返回合并后的值
  3. }

对于多数值为对象的选项,可以使用与 methods 相同的合并策略:

  1. var strategies = Vue.config.optionMergeStrategies
  2. strategies.myOption = strategies.methods

可以在 Vuex 1.x 的混入策略里找到一个更高级的例子:

  1. const merge = Vue.config.optionMergeStrategies.computed
  2. Vue.config.optionMergeStrategies.vuex = function (toVal, fromVal) {
  3. if (!toVal) return fromVal
  4. if (!fromVal) return toVal
  5. return {
  6. getters: merge(toVal.getters, fromVal.getters),
  7. state: merge(toVal.state, fromVal.state),
  8. actions: merge(toVal.actions, fromVal.actions)
  9. }
  10. }