Om *_*tel 10 angularjs typescript angular
app.component.ts文件
import { Component, OnInit } from '@angular/core';
import { Hero } from './hero';
import { HeroService } from './hero.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [HeroService]
})
export class AppComponent implements OnInit{
title = 'Tour of Heroes';
heroes : Hero[ ];
selectedHero : Hero;
constructor(private heroService: HeroService) { }
getHeroes(): void {
this.heroService.getHeroes().then(heroes => this.heroes = heroes);
};
ngOnInit(): void {
this.getHeroes();
};
onSelect(hero: Hero): void{
this.selectedHero = hero;
};
}
Run Code Online (Sandbox Code Playgroud)
hero.service.ts
import { Injectable } from '@angular/core';
import { Hero } from './hero';
import { HEROES } from './mock-heroes';
@Injectable()
export class HeroService {
getHeroes(): Promise<Hero[]> {
return Promise.resolve(HEROES);
}
}
Run Code Online (Sandbox Code Playgroud)
如何解决此错误?
ear*_*rly 17
我得到了同样的错误; 在我搜索了几个答案后,我发现你的代码与我没什么不同,实际上没有错误.只需要重新启动ng服务然后成功编译.参考:Angular2教程:服务中的Promise错误(类型不可分配)
您的this.heroService.getHeroes()
方法返回已解决的承诺.
它应该是
this.heroes = this.heroService.getHeroes();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10343 次 |
最近记录: |