javascript - 覆盖vue中的数据

标签 javascript vue.js mixins vuejs2

我正在使用 mixin 来添加和覆盖 vue 数据。我可以添加新数据,但似乎无法覆盖现有数据。这是简单的代码:

var mixin = {
    data: function() {
    return {
      foo: 'boo',
      test: 'blah'
    }
  }
}

var vm = new Vue({
  el: '#vue-instance',
  mixins: [mixin],
  data: function() {
    return {
      foo: 'bar'
    }
  }
});

当我运行它时,“test”正确地等于“blah”,但“foo”仍然等于“bar”。除了添加新数据之外,是否还可以使用 mixin 覆盖现有的 vue 数据?

这是一个 fiddle :

https://jsfiddle.net/8v9sfxok/

最佳答案

来自mixins documentations :

Options that expect object values ... will be merged into the same object. The component’s options will take priority when there are conflicting keys in these objects

(强调我的)。

如果您想要不同的合并策略,you can define one .

关于javascript - 覆盖vue中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42304671/

相关文章:

javascript - 我需要如何更改这些 TypeScript mixin 类型定义才能允许定义允许类扩展特征的 mixin?

css - Mixins scss 不起作用

javascript - 使用 jquery 获取文本字段的第一个 html 下拉值

vue.js - 如何将 Prop 传递给 Nuxt 中的页面?

html - 使用 less css 的增量文件名

javascript - VueJS 自定义指令函数作为参数

javascript - AJAX 部分加载后 Vue 绑定(bind)?

javascript - 模拟伪随机 LFO 信号建模(Javascript 中的低通滤波)

javascript - Ext JS 树列表存储无限加载?

javascript - 外部 javascript 数组初始化