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

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

TypeError:無法分配給打字稿中對象“[object Array]”的只讀屬性“0”

TypeError:無法分配給打字稿中對象“[object Array]”的只讀屬性“0”

MMTTMM 2023-08-24 17:19:31
我正在研究 Angular 8。我有一個顯示表格的頁面。taskList該表顯示來自組件獲取的對象數(shù)組的數(shù)據(jù)@Input()。我對該表的列有排序功能。我在每一行上還有一個刪除選項。當(dāng)我單擊刪除選項時,它會調(diào)用 api 來刪除該行,然后再次調(diào)用來獲取數(shù)組tasklist。這是同樣的效果  @Effect()  DeleteTask$: Observable<Action> = this.actions$.pipe(    ofType(importActions.DELETE_TASK),    switchMap(params =>      this.globalService        .deleteTask(params)        .mergeMap(deleteSuccess => {          return from([            new importActions.DeleteTaskSuccess(deleteSuccess),            new importActions.LoadTaskList(),          ]);        })        .catch((error, caught) => {          return Observable.of(new GlobalError(error));        }),    ),  );我的問題是,當(dāng)我加載第一頁時,排序功能工作正常。但是,如果我刪除一行然后獲取刪除后的任務(wù)列表,則會收到以下錯誤:ERROR Error: Uncaught (in promise): TypeError: Cannot assign to read only property '0' of object '[object Array]'TypeError: Cannot assign to read only property '0' of object '[object Array]'根據(jù)錯誤消息,我的代碼中的以下函數(shù)給出了錯誤  exchange(a, b) {    const temp = this.taskList[a];    this.taskList[a] = this.taskList[b]; //this line gives error    this.taskList[b] = temp;  }該函數(shù)是使用數(shù)組并對其進行排序的排序代碼的一部分tasklist。流量為ngOnchanges(detects change is taskList array) calls --> this.taskChange('export_name', 'asc') based on some condition calls --> this. exchange(a, b)以下是我的 ngOnchanges 方法ngOnChanges(changes: SimpleChanges) {    if (this.taskList !== null && this.taskList !== undefined) {      this.taskChange('export_name', 'asc');    }  }以下是主要的排序方法  async taskChange(value, taskOrder) {    this.sortOrder = taskOrder;    this.selectedValue = value;    const expr = {      asc: (a, b) => a > b,      desc: (a, b) => a < b,    };我不確定當(dāng)數(shù)組第二次更改時到底是什么導(dǎo)致了此錯誤。我嘗試了很多方法,但沒有一個有效。根據(jù)我在網(wǎng)上發(fā)現(xiàn)的情況,這可能會發(fā)生,因為我正在嘗試改變作為 @Input 接收的數(shù)組。但上面的代碼會改變“tasklist”數(shù)組,但它會在初始頁面加載時起作用。并且僅在數(shù)組更改時停止工作。有人可以幫我嗎?
查看完整描述

3 回答

?
互換的青春

TA貢獻1797條經(jīng)驗 獲得超6個贊

在嘗試對數(shù)組進行排序之前,請嘗試創(chuàng)建該數(shù)組的副本。就像使用展開運算符一樣。

arrayForSort = [...this.taskList]

然后排序后,您可以將其分配回該taskList字段


查看完整回答
反對 回復(fù) 2023-08-24
?
縹緲止盈

TA貢獻2041條經(jīng)驗 獲得超4個贊

對于那些使用 React/Redux 遇到此錯誤消息的人,可能是您試圖直接改變狀態(tài),這是不允許的。

就我而言,我有這樣的設(shè)置來獲取 thunk 中的狀態(tài)(簡化):

import store from "./myStore";


const state = store.getState();

const getItems = state => state.user.items;

const items = getItems(state);

// ↓ this blew up as it was attempting to manipulate `state`

items.sort((a, b) => a.order - b.order);

這是通過以下方式為我解決的:


import store from "./myStore";


const state = store.getState();

const getItems = state => state.user.items;

// ↓ in my case items is an array, so I create a new array by spreading state here

const items = [...getItems(state)];

// ↓ which means we're not manipulating state, but just our `items` array alone

items.sort((a, b) => a.order - b.order);


查看完整回答
反對 回復(fù) 2023-08-24
?
喵喵時光機

TA貢獻1846條經(jīng)驗 獲得超7個贊

我在做一個nextjs項目時遇到了這個確切的錯誤。當(dāng)我收到此錯誤時,我將 a 放在findIndex一個對象數(shù)組上,嘗試將新的鍵值對添加到數(shù)組的特定對象中。所以我只是這樣做了:


const arrayOfObjects = [...someOtheObj.originalArrayKey]

const index = arrayOfObjects.findIndex((obj)=>{

  // I had some conditions here

})

arrayOfObjects[index] = newValue

正確的

const arrayOfObjects = [...someOtheObj.originalArrayKey]

錯誤的

const arrayOfObjects = someOtheObj.originalArrayKey


查看完整回答
反對 回復(fù) 2023-08-24
  • 3 回答
  • 0 關(guān)注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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