简体   繁体   English

VueJS 与 Vuex - 用拼接替换存储在 Vuex 中的数组中的项目(对象)不会重新呈现项目列表

[英]VueJS with Vuex - Replace item (object) in array stored in Vuex with splice doesn't re-render list of items

I have an array of records.我有一系列记录。 Each record is an object with _id (mongo id) , title, and value (value is an object with amount and currency).每条记录都是一个 object ,带有_id (mongo id) 、标题和值(值是一个 object ,带有金额和货币)。

I'm showing the list of records with v-for when ':key' of the list is the id of the record.当列表的 ':key' 是记录的 id 时,我使用 v-for 显示记录列表。

It works when I add a record, (using records.push() in the store), and delete a record (using records.splice(idx, 1) ), But when I'm trying to edit a record and change the title or value.amount the list does not re-render and show the list without the change (using records.splice(idx, 1, updatedRecord))当我添加记录(在商店中使用records.push() )和删除记录(使用records.splice(idx, 1) )时,它可以工作,但是当我尝试编辑记录并更改标题时或 value.amount 列表不会重新渲染并显示没有更改的列表(使用 records.splice(idx, 1, updatedRecord))

Here My Code: Record List这是我的代码:记录列表

 <template> <ion-list> <ion-item> <ion-label>Date</ion-label> <ion-label>Title</ion-label> <ion-label>Value</ion-label> <ion-label /> </ion-item> <RecordPreview v-for="record in records":key="record._id":record="record" @onEditRecord="onEditRecord" @onDeleteRecord="onDeleteRecord" /> </ion-list> </template> <script> import { IonList, IonItem, IonLabel } from '@ionic/vue'; import RecordPreview from './RecordPreview.vue'; export default { name: 'RecordList', components: { RecordPreview, IonList, IonItem, IonLabel, }, props: { records: { type: Array, default: () => [], }, }, emits: ['onEditRecord', 'onDeleteRecord'], setup(_, { emit }) { function onEditRecord(record) { emit('onEditRecord', record); } function onDeleteRecord(record) { emit('onDeleteRecord', record); } return { onEditRecord, onDeleteRecord, }; }, }; </script>

Vuex record module Vuex 记录模块

 import recordService from '../../services/record.service'; export const recordModule = { state: () => ({ records: [], }), actions: { async getRecords({ commit }) { const records = await recordService.getRecords(); commit('setRecords', records); }, async addRecord({ commit }, record) { const newRecord = await recordService.addRecord(record); commit('addRecord', newRecord); }, async updateRecord({ commit }, record) { const res = await recordService.updateRecord(record); if (res.modifiedCount > 0) { commit('updateRecord', record); } }, async removeRecord({ commit }, recordId) { const res = await recordService.removeRecord(recordId); if (res.deletedCount > 0) { commit('removeRecord', recordId); return true; } return false; }, }, mutations: { setRecords(state, records) { state.records = records; }, addRecord(state, record) { state.records.push(record); }, updateRecord(state, updatedRecord) { const recordIdx = state.records.findIndex((rec) => rec._id === updatedRecord._id); state.records.splice(recordIdx, 1, updatedRecord); }, removeRecord(state, recordId) { const recordIdx = state.records.findIndex((rec) => rec._id === recordId); state.records.splice(recordIdx, 1); }, }, getters: { records(state) { return state.records; }, }, };

To get the records I'm using getter from the store placed on the parent component of RecordList like that:要从存储在 RecordList 的父组件上使用 getter 获取记录,如下所示:

 const records = computed(() => store.getters.records);


PS I tried to create a deep copy of the array and replace all the array. PS我试图创建数组的深层副本并替换所有数组。 I also tried to create a deep copy of the record, then change and replace it.我还尝试创建记录的深层副本,然后更改和替换它。 And I think the problem is with the key, coz when I tried to change the:key from 'record._id' to 'record 'it re-render the list when I replaced the item (I guess it work because the pointer of the object was changed)而且我认为问题出在密钥上,因为当我尝试将:密钥从'record._id'更改为'record'时,它会在我替换项目时重新呈现列表(我猜它可以工作,因为object 已更改)

If you are fetching records from an API I recommend that you update by fetching from the API, and if you want to update the item within your state, try this:如果您从 API 获取记录,我建议您通过从 API 获取更新,并且如果您想更新 state 中的项目,请尝试以下操作:

updateRecord(state, updatedRecord) {
  const { title, value } = updatedRecord;
  const recordIdx = state.records.findIndex((rec) => rec._id === updatedRecord._id);

  recordIdx.title = title
  recordIdx.value = value 
  ...
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM