javascript - 重写 Vue 的 mixin 方法

标签 javascript vue.js

有没有办法覆盖一些本地组件在组件内部调用的 npm 的 mixin?

我里面有一个 npm 包组件 node_modules/somePackageName/components/footer.vue 它使用另一个来自 node_modules/somePackageName/mixins/popup.js

popup.js 包含以下方法:

methods: {
  setPopupActiveStatus (val, blur = true) {
    const isVisible = false
  }
},

我想覆盖 App.vue 的行为,我在其中使用 footer.vue 组件,如下所示:

methods: {
  setPopupActiveStatus (val, blur = true) {
    const isVisible = true
  }
},

但它并没有像我想要的那样工作。

====更新====

这是我如何根据@Estradiaz 的回答解决我的问题的解释:

enter image description here

最佳答案

combining mixin & component options :

When a mixin and the component itself contain overlapping options, they will be "merged" using appropriate strategies:

  • Data objects undergo a recursive merge, with the component’s data taking priority in cases of conflicts.

  • Hook functions with the same name are merged into an array so that all of them will be called. Mixin hooks will be called before the component’s own hooks.

  • Methods, Components and Directives will be merged into the same object. The component’s options will take priority when there are conflicting keys in these objects.

这是一个由组件和混合提供的方法的例子:

var mixin = {
  methods: {
    foo: function () {
      console.log('Mixin Msg')
    },
  }
}

var vm = new Vue({
  mixins: [mixin],
  methods: {
    foo: function () {
      console.log('Component Msg')
    },
  }
})

vm.foo() // => "Component Msg"

这是一个 example in codesandbox

所以我相信你应该能够简单地通过在组件上提供一个具有相同名称的方法来“覆盖”mixin,它会优先

关于javascript - 重写 Vue 的 mixin 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877796/

相关文章:

javascript - 将 Angular 代码与外部代码混合的正确范例

javascript - 如何将 axios 请求保存为 json 对象?

if-statement - 如何比较 v-if 与 Vue.js 中的值

javascript - 是否可以在 v-html 指令中放入指令?

javascript - 如何在 Vue.js 中保持可排序数组和组件的顺序

javascript - 未捕获的语法错误 : Unexpected token < into DOCTYPE html PUBLIC

javascript - 不了解此 Canvas 代码的工作原理

java - 日期应该如何发布到java?

node.js - SailsJs 中的全局函数

javascript - 无法在模块外访问 Vuex getter