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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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

2 回答

?
Helenr

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

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

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


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


它不漂亮,但它有效:

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);

  }

}

如果有人有更聰明的方法來(lái)做到這一點(diǎn),我非常渴望聽(tīng)到它!

編輯:

我使用上面發(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

}


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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