rod*_*ner 5 angular-promise angular-cli angular
我正在Angular.io网站上观看“ 英雄的角之旅 ” 教程。我已经使用Angular CLI设置了项目(请参阅本文结尾处的环境版本)。
我陷入第5点(服务):使用教程提供的相同文件,在“ hero.service.ts”的第9行中出现以下错误:
Failed to compile.
/home/myuser/angular-tour-of-heroes/src/app/hero.service.ts (9,4): Type 'Hero[]' is not assignable to type 'Promise<Hero[]>'.
Property 'then' is missing in type '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)
这是在服务承诺解决时起作用的组件(app.component.ts)的片段:
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)
我刚刚在本教程中看到了另一篇关于类似错误的文章,但是该错误描述了关于属性“ length”而不是“ then”的抱怨:
Type 'Promise' is not assignable to type 'Hero[]'. Property 'length' is missing in type 'Promise'.
Run Code Online (Sandbox Code Playgroud)
就像我说的,我被困在这里,不知道我的环境是否与该错误有关。我使用Ubuntu 14.04(ARM的Chromebook crouton的chroot) ,node v7.10.0,npm 4.6.1,:angular 4.1.3angular-cli 1.0.6
$ ng --version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ ? \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.0.6
node: 7.10.0
os: linux arm
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.0.6
@angular/compiler-cli: 4.1.3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1243 次 |
| 最近记录: |