因此,对于本教程本身中的这一特定代码行并没有说太多:

getHeroes(): void {
  this.heroService.getHeroes()
      .subscribe(heroes => this.heroes = heroes) ;

}


从我所看到的,.subscribe从hero.service方法中检索“ HEROES”,并将其传递到.subscribe方法内部的heroes参数中。

至少我认为这是正在发生的事情。如果我使用console.log这样的话,有些特性:

getHeroes(): void {
  this.heroService.getHeroes()
      .subscribe(heroes => console.log(this.heroes )) ;

}


我对“ this.heroes”有一个未定义的

但是,如果我这样console.log:

.subscribe(heroes => console.log(this.heroes = heroes))


我得到它正在调用的数组的输出:

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: 11, name: "Dr Nice"}
1: {id: 12, name: "Narco"}
2: {id: 13, name: "Bombasto"}
3: {id: 14, name: "Celeritas"}
4: {id: 15, name: "Magneta"}
5: {id: 16, name: "RubberMan"}
6: {id: 17, name: "Dynama"}
7: {id: 18, name: "Dr IQ"}
8: {id: 19, name: "Magma"}
9: {id: 20, name: "Tornado"}


更特别的是,如果我在.subscribe方法之外登录console.log this.heroes,那么我也会得到英雄列表,就像这样:

getHeroes(): void {
  this.heroService.getHeroes()
      .subscribe(heroes => this.heroes = heroes),
      console.log(this.heroes) ;

}


输出:

(10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: 11, name: "Dr Nice"}
1: {id: 12, name: "Narco"}
2: {id: 13, name: "Bombasto"}
3: {id: 14, name: "Celeritas"}
4: {id: 15, name: "Magneta"}
5: {id: 16, name: "RubberMan"}
6: {id: 17, name: "Dynama"}
7: {id: 18, name: "Dr IQ"}
8: {id: 19, name: "Magma"}
9: {id: 20, name: "Tornado"}


所以我的问题是,subscribe方法中的英雄发生了什么?它是从服务返回列表,然后将其传递到订阅方法内部的heroes参数吗?

有关本教程的详细信息,请参见:https://angular.io/tutorial/toh-pt4#subscribe-in-heroescomponent

要添加的另一件事是,我认为代码中的heroes:Hero []数组被设置为“ heroes”,但事实并非如此:

import { Component, OnInit } from '@angular/core';
import { Hero } from './hero';
import { HeroService } from '../hero.service';

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {

  heroes: Hero[];

selectedHero: Hero;

onSelect(hero: Hero): void {
  this.selectedHero = hero;
}

getHeroes(): void {
  this.heroService.getHeroes()
      .subscribe(heroes => this.heroes = heroes),
      console.log(this.heroes) ;

}




  constructor(private heroService : HeroService) {



   }

  ngOnInit() {
    this.getHeroes();
  }

}


如果我删除

heroes: Hero[];


从heroes.component内部,我仍然可以获得要显示的英雄列表。因此,this.heroes不与英雄绑定:Hero []

实时示例:https://stackblitz.com/angular/aaldojlqqlvd?file=src%2Fapp%2Fheroes%2Fheroes.component.ts

最佳答案

查看整个heroes.component.ts非常重要,因为您可以看到在顶部定义了一个名为heroes的数组。因此,该函数正在将组件中定义的heroes数组设置为服务返回的heroes数组。使用新名称,可能是这样的:

    import { Component, OnInit } from '@angular/core';

    import { Hero } from '../hero';
    import { HeroService } from '../hero.service';

    @Component({
      selector: 'app-heroes',
      templateUrl: './heroes.component.html',
      styleUrls: ['./heroes.component.css']
    })
    export class HeroesComponent implements OnInit {

      selectedHero: Hero;

      heroesInComponent: Hero[]; // <<<< changed

      constructor(private heroService: HeroService) { }

      ngOnInit() {
        this.getHeroes();
      }

      onSelect(hero: Hero): void {
        this.selectedHero = hero;
      }

      getHeroes(): void {
        this.heroService.getHeroes()
            .subscribe(heroesFromService=> this.heroesInComponent = heroesFromService); // <<<< changed
      }
    }


您的代码段记录未定义的原因是heroesInComponent仍未定义,因为您尚未为其分配值(例如heroesFromService

getHeroes(): void {
  this.heroService.getHeroes()
      .subscribe(heroes => console.log(this.heroesInComponent)) ;
}


此代码段记录正确结果的原因是因为=(赋值)运算符在分配后返回了结果数组。

.subscribe(heroes => console.log(this.heroesInComponent = heroesFromService))

关于javascript - .subscribe方法内部的this.heroes在angular的英雄教程中指的是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56473578/

10-09 21:20