【问题标题】:VueJS modifying arrayVueJS修改数组
【发布时间】:2021-01-12 23:23:22
【问题描述】:

我正在使用 vuex 来存储状态,但我看到了一个奇怪的问题:它似乎正在更改我的一个对象上的 id

在我的操作中,我下载了有关笔记的信息

    saveNote({commit}, noteInfo) {
        var formData = new FormData();
        Object.keys(noteInfo.note).forEach(key => formData.append(key, noteInfo.note[key]));
        return axios.post("/notes/saveText/" + noteInfo.note.id, formData).then(res => commit('SAVE_NOTE', res.data)).catch(err => console.log(err));
    },

导致以下 json 有效负载:

{
   "success":1,
   "notes":[
      {
         "id":38,
         "audio_length":null,
         "ap_id":null,
      },
      {
         "id":39,
         "audio_length":null,
         "ap_id":null,
      }
   ],
   "updated":39
}

我将其截断了一点,只是为了显示重要部分。然后我运行突变,如下

SAVE_NOTE(state, data) {
        console.log("SAVENOTES", data.notes);
        state.currentPatient.notes = data.notes;
        state.currentNote = state.currentPatient.notes.find(n => n.id = data.updated);
    },

但是,当我打开我的 chrome 调试时,我得到了这个:

SAVENOTE (2) [{…}, {…}]
0:
ap_id: (...)
audio_length: (...)
id: 39                                 <---- This one should be 38!
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get ap_id: ƒ reactiveGetter()
set ap_id: ƒ reactiveSetter(newVal)
get audio_length: ƒ reactiveGetter()
set audio_length: ƒ reactiveSetter(newVal)
get id: ƒ reactiveGetter()
set id: ƒ reactiveSetter(newVal)
__proto__: Object
1:
ap_id: (...)
audio_length: (...)
id: 39
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get ap_id: ƒ reactiveGetter()
set ap_id: ƒ reactiveSetter(newVal)
....

知道为什么将 id 从 38 更改为 39?

【问题讨论】:

    标签: json laravel vue.js axios vuex


    【解决方案1】:

    在 SAVE_NOTE 突变中,您的最后一行是 n.id = data.updated

    通过查看您的 json 有效负载,它看起来像 data.updated = 39。所以您正在设置 n.id = 39

    【讨论】:

    • 就是这样!!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2021-11-23
    • 2023-02-20
    • 2019-04-05
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多