自动对象变异Vuejs

时间:2018-09-28 16:14:43

标签: vue.js binding mutation

我有一个小问题。 我正在使用Vuex,并且我具有对象类型的“用户”状态,当我从组件中调用它并将其分配给模型时,一切正常,但是当对模型进行更改时,我会自动更改为我一直是“用户”,我不希望这种情况发生。enter image description here

1 个答案:

答案 0 :(得分:1)

您可以将vuex状态连接到带有计算的set和get的v模型。 在get中,您应该编写一个函数,该函数从存储中返回所需的数据。 在集合中,您应该编写一个向商店提交一个变异的函数。

vuex docs鼓励开发人员以这种方式处理表格。

{
  template : '<input v-model="username"',
  computed: {
   username: {
    get: function () {return this.$store.user.name},
    set: function (newVal) { this.$store.commit('setNewName', newVal)}
    }
  }
}
相关问题