typescript – Angular2没有NameService的提供程序
发布时间:2020-12-17 09:11:31  所属栏目:安全  来源:网络整理 
            导读:我有问题加载类到Angular2组件。我试图解决很长时间,甚至试图将其添加到单个文件。我有的是: 应用程序 /// reference path="../typings/angular2/angular2.d.ts" /import {Component,View,bootstrap,NgFor} from "angular2/angular2";import {NameService}
                
                
                
            | 
 我有问题加载类到Angular2组件。我试图解决很长时间,甚至试图将其添加到单个文件。我有的是: 
  
  应用程序 /// <reference path="../typings/angular2/angular2.d.ts" />
import {Component,View,bootstrap,NgFor} from "angular2/angular2";
import {NameService} from "./services/NameService";
@Component({
    selector:'my-app',injectables: [NameService]
})
@View({
    template:'<h1>Hi {{name}}</h1>' +
    '<p>Friends</p>' +
    '<ul>' +
    '   <li *ng-for="#name of names">{{name}}</li>' +
    '</ul>',directives:[NgFor]
})
class MyAppComponent
{
    name:string;
    names:Array<string>;
    constructor(nameService:NameService)
    {
        this.name = 'Michal';
        this.names = nameService.getNames();
    }
}
bootstrap(MyAppComponent);services / NameService.ts export class NameService {
    names: Array<string>;
    constructor() {
        this.names = ["Alice","Aarav","Martín","Shannon","Ariana","Kai"];
    }
    getNames()
    {
        return this.names;
    }
}所有的时间,我得到错误消息说“没有提供者NameService”…有人可以帮助我发现我的代码的小问题吗? 
 你必须使用提供程序,而不是注射 
  
  
  @Component({
    selector: 'my-app',providers: [NameService]
})Complete code sample here。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
