[英]How do I update const array in Angular?
親愛的團隊成員,
我是 angular 的新手。
我有一個 ts 文件。
export const HEROES: Hero[] = [
{ id: 11, name: 'Dr Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
updateHero(hero:Hero) : void{
let updateItem = this.heroes.find(x=> x.id == hero.id);
let index = this.heroes.indexOf(updateItem);
this.heroes[index] = hero;
}
如何更新 HEROES 文件?
我的目標是 select 一個英雄,編輯名字然后更新 const 文件。 我知道它應該來自服務器。 但是為了玩?
謝謝。
一旦聲明,您就不能在 const 中分配/更新值。 所以你需要將 const 類型更改為另一種類型。
默認情況下 javascript 常量行為https://www.w3schools.com/js/js_const.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.