javascript - 如何在 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 文件。我知道它应该来自服务器。但是为了玩?
谢谢。
解决方案
一旦声明,您就不能在 const 中分配/更新值。所以你需要将 const 类型更改为另一种类型。
默认情况下 javascript const 行为 https://www.w3schools.com/js/js_const.asp
推荐阅读
- javascript - 我需要在特定行拆分一个二维数组并将它们变成两个新的二维数组(nodejs)
- javascript - 将搜索数据导出为 PDF/打印
- java - 插入表时如何访问值?
- javascript - 通过 selectize.js 设置 select 的初始值?
- c++ - 在 C++ 中修改字符串的 BigO 时间复杂度是多少?
- yii2 - 如何在 yii2 模型中添加错误函数
- django - 如何在添加表单 django 期间从管理视图中删除字段
- sql-server - 识别 AWS RDS 上的 SQL Server 死锁
- mysql - 是否有可能,如果数据库中的表被更新或更改,它会自动转储到 .sql 文件中?
- flutter - 使用印度域时 Flutter Project 未运行