angular - 为什么这个 (gameindex) 属性值在由 console.log 打印时未定义?
问题描述
在代码中 console.log(this.gameIndex) 打印出 undefined。为什么是这样?当我在构造函数中使用 console.log(this.gameIndex) 时,会打印出正确的值。
打字稿
export class HomeComponent implements OnInit {
game:String;
gameIndex;
games:String[];
constructor() {
this.gameIndex = 0;
this.games = ['game1','game2','game3'];
this.game = this.games[this.gameIndex];
setInterval(this.rotatingBackground,3000);
console.log(this.gameIndex);
}
ngOnInit() {
}
rotatingBackground():any {
console.log(this.gameIndex);
this.game = this.games[this.gameIndex];
console.log(this.game);
}
}
解决方案
那是因为你没有定义它。
gameIndex;
应该
gameIndex:Number;`
推荐阅读
- java - 更改 org.gradle.jvmargs 值
- java - 我怎样才能改变firebase的规则?
- r - 自动将直方图写入 r 中的外部文件夹
- javascript - 如何在革命 silider 中添加转化跟踪代码 onclick 图像?
- python - 使用 Selenium (Python) 在 Instagram 上撰写帖子的标题
- python - 如何将列的值拆分为数据框中的不同列
- java - 使用 Gradle 构建 Java 程序时,有没有办法运行 JS 程序?
- c# - C# 和 MySql 与 MySql 的连接是否安全?
- sql - PostgreSQL group by 然后按值计数
- ios - iPhone 7 上的谷歌地图 API