繁体   English   中英

角2提供()与ES5中的字符串?

[英]Angular 2 provide() with string in ES5?

我正在尝试使用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. 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中起作用:/

根据Inject文档

当@Inject()不存在时,Injector将使用参数的类型注释。

在您的情况下,您要注入一个简单的字符串,但是angular2不能将字符串用作您的Stuff类型。 因此,在这些情况下,您必须使用ng.core.Inject ,如下所示

.Class({

    // Note the ng.core.Inject
    constructor: [ng.core.Inject("Stuff"), function(stuff) {
      console.log(stuff);
    }]
});

这是您的代码正常工作的plnkr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM