亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Vue.js-Vuex 更新狀態數組中的對象未反映在組件 DOM 中

Vue.js-Vuex 更新狀態數組中的對象未反映在組件 DOM 中

繁花如伊 2023-01-06 15:42:06
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 更新。”
查看完整描述

1 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

你有一個反應警告,所以你應該使用Vue.set函數:


x(state, data) {  
          Vue.set(state.todos,0,data);
        }

有關更多詳細信息,請檢查


查看完整回答
反對 回復 2023-01-06
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號