【问题标题】:State object cannot re assign by payload objects in mutations状态对象不能由突变中的有效负载对象重新分配
【发布时间】:2021-11-18 05:48:25
【问题描述】:

我有这个状态对象,有默认值

state() {
return {
  projects: [
    { code: "01", name: "test" },
    { code: "01", name: "test" },
    { code: "01", name: "test" },
  ],
 };
},

我正在尝试将其替换为来自操作的有效负载,

mutations: {
 setProject(state,payload) {
  state.projects = payload;
 }
},

问题是当我尝试使用 console.log 时它不会替换 state.projects 对象 我正在使用 vuex 3.6.2 我的有效负载是这样的

[
 { code: "01", name: "Picture" },
 { code: "02", name: "Perfect" },
],

【问题讨论】:

    标签: vuex


    【解决方案1】:

    尝试将状态作为对象而不是函数。例如

    const state = {
      projects: [
        { code: "01", name: "test" },
        { code: "01", name: "test" },
        { code: "01", name: "test" },
      ],
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 2017-07-28
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 2018-07-01
      相关资源
      最近更新 更多