【发布时间】:2016-05-28 19:47:40
【问题描述】:
当我拥有嵌套在根组件providing 下的组件时,我无法实例化我的应用程序,该组件在其构造函数中使用。
import {Component, Injectable} from 'angular2/core';
@Component()
export class GrandChild(){
constructor () {
this.hello = "hey!"
}
}
@Component({
providers: [GrandChild]
})
@Injectable()
export class Child {
constructor(public grandchild: GrandChild) {
this.grandchild = grandchild;
}
}
@Component({
providers: [Child],
template: `Everything rendered fine!`,
selector: 'app'
})
export class App {
kid: Child;
constructor(public child: Child) {
this.kid = child;
}
}
EXCEPTION: No provider for GrandChild! (App -> Child -> GrandChild)
我想知道为什么这种行为是非法的。假设我想在我的Child 类中使用GrandChild 类,并在我的App 类中简单地引用Child 类。这似乎是不可能的。
创建provide 层次结构的正确方法是什么?
非常感谢。
【问题讨论】:
标签: angularjs typescript angular