【发布时间】:2021-12-19 13:54:51
【问题描述】:
完整的错误信息:
元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“Igame”类型。 在“Igame”类型上找不到带有“字符串”类型参数的索引签名。 ts(7053)
界面:
export interface Igame {
id: number;
isStart: boolean;
isFinish: boolean;
}
在类组件中
public games: Igame[] = [];
public gameAction(id: number, action: string): void {
const index = this.games.findIndex((game) => game.id === id);
this.games[index][action] = true;
}
错误在这段代码下划线:
this.games[index][action]
函数gameAction用于改变游戏状态。 例如
gameAction(1, 'isStart')
(游戏 #1 开始)
【问题讨论】:
-
你到底想用 this.games[index][action] 这一行做什么?
-
函数gameAction用于改变游戏状态。例如 gameAction(1, 'isStart') (游戏 #1 开始)
标签: angular typescript