Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在订阅中指定对象_Angular - Fatal编程技术网

Angular 在订阅中指定对象

Angular 在订阅中指定对象,angular,Angular,我遵循的是来自的教程,但我试图通过使用“card”而不是“hero”来稍微调整heros.component.ts类。当我试图调用.subscribe()时,我得到 src/app/card/card.component.ts(12,19)中出错:错误TS1109:应为表达式 我的版本(注意cardlist变量名称更改) 下面是我认为在getCards()函数中发生的情况: 方法getCards()使用返回类型void声明 调用cardService(在文件顶部导入)以使用getCards()方

我遵循的是来自的教程,但我试图通过使用“card”而不是“hero”来稍微调整heros.component.ts类。当我试图调用
.subscribe()
时,我得到

src/app/card/card.component.ts(12,19)中出错:错误TS1109:应为表达式

我的版本(注意cardlist变量名称更改)

下面是我认为在getCards()函数中发生的情况:

  • 方法
    getCards()
    使用返回类型void声明
  • 调用cardService(在文件顶部导入)以使用
    getCards()
    方法
  • 调用
    .subscribe()
    以侦听对该数据的更改
  • 此位:
    c=>this.cardlist=c
    表示从服务传入的每一张卡
    c
    都将其添加到
    cardlist
  • 显然,我对第4步的看法是错误的。我做错了什么?


    工作指南代码(为方便起见复制):


    你少了一些妄想症,试一下如下

       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);
      });
    }