vue.js - 当一个项目有一些变化时更新 vuex 数组的状态

标签 vue.js vuejs2 vuex

我的 vuex 存储中有一个名为 cases 的数组。

当我更新数组中现有项目中的几个字段时,我想用新内容更新数组。

我以为我可以在我的突变中做这样的事情但是不起作用并得到错误 typeError: undefined is not an object (evaluating 'state.objects.find')

EDIT_CASE (state, payload) {
    const item = state.objects.find(item => item.id === payload.recordId);
Object.assign(item, payload.case_status);

我的数组如下:

[
    {
        "case_name": "Laptop not working",
        "case_status": "live",
        "case_summary": "This is some summary content",
        "createdBy": "zippy",
        "createdDate": "2018-06-21T15:20:22.932Z",
        "id": "-LFXvk9yY5c-O8yIdf8k"
    },
    {
        "case_name": "Something else",
        "case_status": "live",
        "case_summary": "This is some summary content",
        "createdBy": "zippy",
        "createdDate": "2018-06-21T15:20:22.932Z",
        "id": "-STVvk9yY5c-O3yiTy8k"
    }
]

我还认为,从我读到的内容来看,Vue 不会观察到数组内的变化,所以我可能会完全错误地处理这个问题,需要删除然后重新添加数组项?

基本上我有一个列表,我对后端进行了更改,现在我希望该列表反射(reflect)我通过更新案例状态所做的更改,有人可以帮忙吗?

最佳答案

您的示例没有数组问题,因为您尝试更改对象属性 - 而不是数组元素引用。 问题出在 Object.assign(item, payload.case_status); - 您应该提供一个对象,而不仅仅是一个字段。 (你还说那个数组叫 cases 但例子有 objects,也许这也是问题);

所以这应该可行:

EDIT_CASE (state, payload) {
    const item = state.objects.find(item => item.id === payload.recordId);
    Object.assign(item, payload);
}

错误:

undefined is not an object

我认为,它与 Object.assign 有关,因为您向它传递的字段可能是 undefined

附言有一个小示例可以帮助您了解何时出现数组问题以及何时一切正常。查看代码注释:)

new Vue({
  el: "#app",
  data: {
    todos: [
      { text: "Learn JavaScript" },
      { text: "Learn Vue" },
      { text: "Play around in JSFiddle" },
      { text: "Build something awesome" }
    ]
  },
  methods: {
    // work because object property is reactive
  	changeItemProperty() {
    	this.todos[3].text = "changedItemProperty";
    },
    // same reason, properties are reactive
    changeItemWithAssign() {
    	Object.assign(this.todos[3], { text: "changedItemWithAssign" });
    },
    // does not work, can not track changes in array
    // also this will break all attempts to change TEXT property in UI
    // because property becomes not reactive after this due to new object
    // try to changeItemProperty or  changeItemWithAssign - does not work!
    // changeItem2 will fix it :)
    changeItem() {
    	this.todos[3] = { text: "changedItem" }
    },
    // works
    changeItem2() {
    	Vue.set(this.todos, 3, { text: "changedItem2" });
    }	
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.min.js"></script>
<div id="app">
  <div v-for="todo in todos" :key="todo.text">
     {{todo.text}}
  </div>
  <button @click="changeItemProperty">changeItemProperty(works)</button>
  <button @click="changeItemWithAssign">changeItemWithAssign(works)</button>
  <button @click="changeItem">changeItem(does not work!)</button>
  <button @click="changeItem2">changeItem2(works)</button>
</div>

关于vue.js - 当一个项目有一些变化时更新 vuex 数组的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51149729/

相关文章:

javascript - 错误信息。 "Props with type Object/Array must use a factory function to return the default value."

vue.js - 在调整屏幕大小之前,无法使用 VueJs 和传单在 DIV 上显示所有 map

javascript - 如何在 Vuex 的 Action 中调用 Action

javascript - 当我在分派(dispatch)后更新 vue 组件中的状态时,防止状态更新

javascript - 列表渲染中的 Vuex 和 react 性

javascript - 为什么我的组件没有导入到 App.vue 中?

javascript - 可滚动元素中的下拉菜单

javascript - 在 Vue.JS 中观察动态嵌套数据?

css - 如何自定义自动完成 View ?

mysql - 我无法将输入保存到数据库 Vuejs 和 Laravel