Angular 在订阅中指定对象
我遵循的是来自的教程,但我试图通过使用“card”而不是“hero”来稍微调整heros.component.ts类。当我试图调用Angular 在订阅中指定对象,angular,Angular,我遵循的是来自的教程,但我试图通过使用“card”而不是“hero”来稍微调整heros.component.ts类。当我试图调用.subscribe()时,我得到 src/app/card/card.component.ts(12,19)中出错:错误TS1109:应为表达式 我的版本(注意cardlist变量名称更改) 下面是我认为在getCards()函数中发生的情况: 方法getCards()使用返回类型void声明 调用cardService(在文件顶部导入)以使用getCards()方
.subscribe()
时,我得到
src/app/card/card.component.ts(12,19)中出错:错误TS1109:应为表达式
我的版本(注意cardlist变量名称更改)
下面是我认为在getCards()函数中发生的情况:
getCards()
使用返回类型void声明getCards()
方法.subscribe()
以侦听对该数据的更改c=>this.cardlist=c
表示从服务传入的每一张卡c
都将其添加到cardlist
工作指南代码(为方便起见复制):
你少了一些妄想症,试一下如下
getHeroes(): void {
this.heroService.getHeroes()
.subscribe(resUserData => {
this.heroes = resUserData
});
}
你少了一些妄想症,试一下如下
getHeroes(): void {
this.heroService.getHeroes()
.subscribe(resUserData => {
this.heroes = resUserData
});
}
原来是语法错误
cardlist=Card[]代码>应该是卡片列表:卡片[]代码>原来是一个语法错误cardlist=Card[]代码>应该是卡片列表:卡片[]代码>
getHeroes(): void {
this.heroService.getHeroes()
.subscribe(resUserData => {
this.heroes = resUserData
});
}
export class CardComponent implements OnInit {
cardlist = Card[];
....
getCards(): void {
this.cardService.getCards()
.subscribe(c => { this.cardlist = c);
});
}