Vuex/商店:import Vue from 'vue';import Vuex from 'vuex';Vue.use(Vuex);const store = new Vuex.Store({ state: { todos: [ {id: 1, text: 'Clean kitchen', done: false}, {id: 2, text: 'Clean bedroom', done: false}, {id: 3, text: 'Clean bathroom', done: false}, {id: 3, text: 'Clean clothes', done: true}, ], }, mutations: { x(state, data) { state.todos[0] = data; } },});export default store;測試視圖:<template><div class="container"> <ul> <li v-for="(item, i) in todos" :key="i" > <span class="description">{{ item.text }}</span> <span class="status">{{ item.status }}</span> </li> </ul> <button @click="x">Change object</button></div></template><script>import { mapState } from 'vuex';export default { methods: { x() { this.$store.commit('x', {id: 34, text: 'Celebrate birthday', done: false}); } }, computed : { ...mapState(['todos']), },}</script>單擊“更改對象”按鈕時,我可以在 Vue Dev Tools 中看到商店的狀態已更改,但組件 Test.vue 顯示的數據沒有更改,第一個 li 項目“Clean kitchen”仍然存在。當我在 Vue Dev Tools 中提交更改時,更改發生了。不確定我做錯了什么。狀態https://vuex.vuejs.org/guide/state.html “每當 store.state.count 發生變化時,它都會導致計算屬性重新評估,并觸發相關的 DOM 更新。”
Vue.js-Vuex 更新狀態數組中的對象未反映在組件 DOM 中
繁花如伊
2023-01-06 15:42:06