我正在尝试使用String作为提供而不是类的标记:
app.RandomComponent = ng.core
.Component({
selector: "randomComponent",
template: "Component!",
providers: [
ng.core.provide("Stuff", {useValue: "Hello"})
]
})
.Class({
constructor: ["Stuff", function(stuff) {
}]
});
然而,它会引发错误:EXCEPTION: Cannot resolve all parameters for 'class5'(Stuff). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'class5' is decorated with Injectable.
根据https://angular.io/docs/ts/latest/api/core/Provider-class.html,它至少应该在TS中起作用:/
答案 0 :(得分:1)
当@Inject()不存在时,Injector将使用参数的类型注释。
在您的情况下,您注入了一个简单的字符串,但angular2不能使用字符串作为 Stuff 的类型。因此,在这些情况下,您必须使用ng.core.Inject
,如下所示
.Class({
// Note the ng.core.Inject
constructor: [ng.core.Inject("Stuff"), function(stuff) {
console.log(stuff);
}]
});
这里有plnkr代码正常运行。