我正在尝试遵循官方教程.一切顺利,直到这里的路由部分.当我到达我们重新制作app.component.ts并更改app.module.ts的部分时,我在localhost上获得了一个加载屏幕,控制台错误是:
未处理的Promise拒绝:模板解析错误:无法绑定到'hero',因为它不是'my-hero-detail'的已知属性.
我确保通过从这里复制粘贴相关文件,这可能是我之前在教程中所犯的错误.该项目的工作原理与喷射器完全相同.
我的问题是什么导致承诺失败,当它在早期版本中工作,我在哪里可以学习如何解决它?
代码摘录:
来自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>
`,
Run Code Online (Sandbox Code Playgroud)
...
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; }
}
Run Code Online (Sandbox Code Playgroud)
来自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';
}
Run Code Online (Sandbox Code Playgroud)
来自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 {
}
Run Code Online (Sandbox Code Playgroud)
来自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;
}
Run Code Online (Sandbox Code Playgroud)
已编辑以修复plunkr链接
小智 23
我在教程后面得到了同样的错误.他们可能错过了一点变化app.module.ts.在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 { }
Run Code Online (Sandbox Code Playgroud)
我假设他们忘记记录这一变化.如果您查看下一章(https://angular.io/docs/ts/latest/tutorial/toh-pt6.html),您会在app.module.js文件中找到此更改.
如果你读下几段(更像是30段),你会看到
使用<my-hero-detail>标记删除模板的最后一行.
这将停止该错误
我现在正在阅读教程,实际上是在寻找答案哈哈
| 归档时间: |
|
| 查看次数: |
7071 次 |
| 最近记录: |