Angular 2 教程,路由部分未处理的承诺拒绝

2023-12-26

我正在尝试遵循官方教程。一切都很顺利,直到路由部分here https://angular.io/docs/ts/latest/tutorial/toh-pt5.html。当我到达重新制作 app.component.ts 并更改 app.module.ts 的部分时,我在本地主机上看到一个加载屏幕,控制台错误是:

未处理的 Promise 拒绝:模板解析错误: 无法绑定到“英雄”,因为它不是“我的英雄细节”的已知属性。

我通过复制粘贴相关文件来确保这不是我之前在教程中可能犯的错误here https://angular.io/resources/live-examples/toh-4/ts/plnkr.html。该项目的工作原理与 plunker 上所示的完全一样。

我的问题是,是什么导致承诺在早期版本中运行时失败,我在哪里可以学习如何修复它?

代码摘录:

来自 app.component.ts

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

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

@Component({
selector: 'my-heroes',
template: `
<h1>{{title}}</h1>
<h2>My Heroes</h2>
<ul class="heroes">
  <li *ngFor="let hero of heroes"
    [class.selected]="hero === selectedHero"
    (click)="onSelect(hero)">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>
<my-hero-detail [hero]="selectedHero"></my-hero-detail>
`,

...

export class HeroesComponent implements OnInit {
title = 'Tour of Heroes';
heroes: Hero[];
selectedHero: Hero;

constructor(private heroService: HeroService) { }

getHeroes() {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
}

ngOnInit() {
  this.getHeroes();
}

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

来自 app.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'my-app',
  template: `
    <h1>{{title}}</h1>
    <my-heroes></my-heroes>
  `
})
export class AppComponent {
  title = 'Tour of Heroes';
}

来自 app.module.ts

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { AppComponent }   from './app.component';
import { HeroesComponent }  from './heroes.component';
import { HeroService }  from './hero.service';
@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent,
    HeroesComponent
  ],
  providers: [
    HeroService
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

来自 Hero-detail.component.ts

import { Component, Input } from '@angular/core';
import { Hero } from './hero';

@Component({
  selector: 'my-hero-detail',
  template: `
    <div *ngIf="hero">
      <h2>{{hero.name}} details!</h2>
      <div>
        <label>id: </label>{{hero.id}}
      </div>
      <div>
        <label>name: </label>
        <input [(ngModel)]="hero.name" placeholder="name"/>
      </div>
    </div>
  `
})
export class HeroDetailComponent {
  @Input() hero: Hero;
}

已编辑修复 plunkr 链接


按照教程我遇到了同样的错误。他们可能会错过一些小变化app.module.ts. The HeroDetailComponent尚未导入并添加到声明数组中。因此财产hero该组件的组成未知。

添加导入以及相应的声明应该可以解决此问题:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';

import { AppComponent }  from './app.component';

import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';

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

@NgModule({
    imports: [
        BrowserModule,
        FormsModule
    ],
    declarations: [
        AppComponent,
        HeroesComponent,
        HeroDetailComponent
    ],
    providers: [
        HeroService
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

我认为他们忘记记录这一变化。如果你看一下下一章(https://angular.io/docs/ts/latest/tutorial/toh-pt6.html https://angular.io/docs/ts/latest/tutorial/toh-pt6.html)你会发现这个变化app.module.js file.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 2 教程,路由部分未处理的承诺拒绝 的相关文章

随机推荐