我甚至沒有使用FooService,但Angular 2想抱怨。承諾拒絕:FooService未定義
export class DisplayFooComponent implements OnInit {
private foo: Foo;
constructor() {}
ngOnInit() {
// this.getFoo();
this.foo.name = "Bar Test";
}
// getFoo() : void {
// this.fooService.getFoo()
// .then(foo => this.foo = foo);
// }
}
我追了幾個小時。引用服務一切都消失了,但它仍然抱怨說,它沒有定義,(因爲角2示數了),它不會顯示在頁面上的任何
但是,如果我做的:
export class DisplayFooComponent implements OnInit {
private foo: Foo;
constructor(private fooService:FooService) {}
ngOnInit() {
this.getFoo();
//this.foo.name = "Bar Test";
}
getFoo() : void {
this.fooService.getFoo()
.then(foo => this.foo = foo);
}
}
我仍然得到一個錯誤,該服務未定義,但它使用我的服務,進行http調用,仍然獲得Foo。
我想我錯過了一些非常簡單的東西。任何幫助是極大的讚賞。
我也有這些,我只是沒有將它們包含在代碼片段中。 – MaxPower