我在Angular2官方网站上试过这个例子:https://angular.io/docs/ts/latest/guide/pipes.html
未显示任何消息且未显示任何错误。
import {Component} from 'angular2/core';
// Initial view: "Message: "
// After 500ms: Message: You are my Hero!"
@Component({
selector: 'hero-message',
template: 'Message: {{delayedMessage | async}}',
})
export class HeroAsyncMessageComponent {
delayedMessage:Promise<string> = new Promise((resolve, reject) => {
setTimeout(() => resolve('You are my Hero!'), 500);
});
}
以下是plunker:https://plnkr.co/edit/SokJZ8cGGx4upIaG7wW8?p=catalogue
答案 0 :(得分:2)
你错过了构造函数
export class AppComponent {
data: Promise<string>;
constructor() { // << You missed it!
this.data = new Promise((resolve,reject)=>{
setTimeout(() => resolve("Hello World "), 500);
});
}
}
或者您也可以直接将Promise分配给属性
data: Promise<string> = new Promise((resolve,reject)=>{
setTimeout(() => resolve("Hello World "), 500);
});
这是您的plnkr工作。