首页 > 解决方案 > 如何在 Angular 中更新 const 数组?

问题描述

亲爱的团队成员,

我是角度的新手。

我有一个 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 文件?

我的目标是选择一个英雄,编辑名称,然后更新 const 文件。我知道它应该来自服务器。但是为了玩?

谢谢。

标签: javascripthtmlangular

解决方案


一旦声明,您就不能在 const 中分配/更新值。所以你需要将 const 类型更改为另一种类型。

默认情况下 javascript const 行为 https://www.w3schools.com/js/js_const.asp


推荐阅读