我在試圖瞭解@Host
裝飾器如何工作與視圖組件解耦。所以,我創建了以下注入樹:@Host裝飾器如何在注入器樹中工作
class Dependency {
}
@Injectable()
class NeedsDependency {
constructor(@Host() public dependency: Dependency) {
console.log(this.dependency); // outputs 'A', but I expected the error
}
}
const parent = ReflectiveInjector.resolveAndCreate([{provide: Dependency, useValue: 'A'}]);
const child = parent.resolveAndCreateChild([]);
const grandchild = child.resolveAndCreateChild([NeedsDependency]);
grandchild.get(NeedsDependency);
我希望得到一個錯誤,因爲我瞭解grandchild
噴油器的主機child
,並沒有在child
噴油器提供Dependency
。但是,當我運行此代碼時,我從根注入器中注入了'A'
。爲什麼?
提供了'NeedsDependency'('NgModel'或'Component')? –
在這個例子中沒有角度框架,它是去耦的 –
然後你不應該期望'@Host()'有任何效果。 –