第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Vuex:無法更改動作內(nèi)深度嵌套的狀態(tài)數(shù)據(jù)

Vuex:無法更改動作內(nèi)深度嵌套的狀態(tài)數(shù)據(jù)

阿晨1998 2021-10-14 14:06:32
在商店中,我有一個更新一些數(shù)據(jù)的操作,該操作如下所示:setRoomImage({ state }, { room, index, subIndex, image }) {      state.fullReport.rooms[room].items[index].items[subIndex].image = image;      console.log(state.fullReport.rooms[room].items[index].items[subIndex])    },因?yàn)樗羞@些數(shù)據(jù)都是動態(tài)的,所以我必須動態(tài)更改嵌套值并且不能直接對屬性進(jìn)行硬編碼。數(shù)據(jù)如下所示:fullreport: {    rooms: {        abc: {          items: [            {              type: "image-only",              items: [                {                  label: "Main Image 1",                  image: ""                },                {                  label: "Main Image 2",                  image: ""                }              ]            }          ]        }      }}當(dāng)我分派動作時,在控制臺中我可以看到子屬性的值image已成功改變,但是如果我從 Chrome 中的 Vue DevTools 訪問 VueX 存儲,我看到該值在那里沒有改變。這是控制臺輸出:拜托,有人能告訴為什么會這樣嗎?據(jù)我所知,數(shù)據(jù)正在成功更改,但不知何故狀態(tài)沒有顯示它,因此我的組件不會重新渲染。我也嘗試使用Vue.set而不是簡單的賦值,但仍然沒有運(yùn)氣:(Vue.set(  state.fullReport.rooms[room].items[index].items[subIndex],  "image",   image );編輯:按照David Gard 的回答,我嘗試了以下操作:我也在使用 Lodash _(我知道制作對象的整個副本不好),這是變異代碼塊。let fullReportCopy = _.cloneDeep(state.fullReport);fullReportCopy.rooms[room].items[index].items[subIndex].image = image;Vue.set(state, "fullReport", fullReportCopy);現(xiàn)在在計(jì)算屬性中,它state.fullReport是一個依賴項(xiàng),我有一個console.log只要重新計(jì)算計(jì)算屬性就會打印出一個字符串。每次我提交這個變更時,我都會看到計(jì)算屬性記錄字符串,但它接收的狀態(tài)仍然沒有改變,我猜Vue.set只是告訴計(jì)算屬性狀態(tài)改變了,但它實(shí)際上并沒有改變它。因此,我的組件的 UI 沒有變化。
查看完整描述

2 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個贊

正如評論中提到的 - 如果您在商店中持有深度嵌套的狀態(tài),它很快就會變得復(fù)雜。

問題是,您必須以兩種不同的方式填充數(shù)組和對象,因此,請考慮是否需要訪問它們的本機(jī)方法。不幸的是,Vuex 還不支持反應(yīng)式地圖。


除此之外,我還處理需要動態(tài)設(shè)置具有多個嵌套級別的屬性的項(xiàng)目。一種方法是遞歸設(shè)置每個屬性。


它不漂亮,但它有效:

function createReactiveNestedObject(rootProp, object) {

// root is your rootProperty; e.g. state.fullReport

// object is the entire nested object you want to set


  let root = rootProp;

  const isArray = root instanceof Array;

  // you need to fill Arrays with native Array methods (.push())

  // and Object with Vue.set()


  Object.keys(object).forEach((key, i) => {

    if (object[key] instanceof Array) {

      createReactiveArray(isArray, root, key, object[key])

    } else if (object[key] instanceof Object) {

      createReactiveObject(isArray, root, key, object[key]);

    } else {

      setReactiveValue(isArray, root, key, object[key])

    }

  })

}


function createReactiveArray(isArray, root, key, values) {

  if (isArray) {

    root.push([]);

  } else {

    Vue.set(root, key, []);

  }

  fillArray(root[key], values)

}


function fillArray(rootArray, arrayElements) {

  arrayElements.forEach((element, i) => {

    if (element instanceof Array) {

      rootArray.push([])

    } else if (element instanceof Object) {

      rootArray.push({});

    } else {

      rootArray.push(element);

    }

    createReactiveNestedFilterObject(rootArray[i], element);

  })

}


function createReactiveObject(isArray, obj, key, values) {

  if (isArray) {

    obj.push({});

  } else {

    Vue.set(obj, key, {});

  }

  createReactiveNestedFilterObject(obj[key], values);

}


function setValue(isArray, obj, key, value) {

  if (isArray) {

    obj.push(value);

  } else {

    Vue.set(obj, key, value);

  }

}

如果有人有更聰明的方法來做到這一點(diǎn),我非??释牭剿?/p>

編輯:

我使用上面發(fā)布的解決方案的方式是這樣的:


// in store/actions.js


export const actions = {

  ...

  async prepareReactiveObject({ commit }, rawObject) {

    commit('CREATE_REACTIVE_OBJECT', rawObject);

  },

  ...

}

// in store/mutations.js

import { helper } from './helpers';


export const mutations = {

  ...

  CREATE_REACTIVE_OBJECT(state, rawObject) {

    helper.createReactiveNestedObject(state.rootProperty, rawObject);

  },

  ...

}

// in store/helper.js


// the above functions and


export const helper = {

  createReactiveNestedObject

}


查看完整回答
反對 回復(fù) 2021-10-14
  • 2 回答
  • 0 關(guān)注
  • 309 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號