typescript – Angular2没有NameService的提供程序

前端之家收集整理的这篇文章主要介绍了typescript – Angular2没有NameService的提供程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有问题加载类到Angular2组件。我试图解决很长时间,甚至试图将其添加到单个文件。我有的是:

应用程序

  1. /// <reference path="../typings/angular2/angular2.d.ts" />
  2.  
  3. import {Component,View,bootstrap,NgFor} from "angular2/angular2";
  4. import {NameService} from "./services/NameService";
  5.  
  6. @Component({
  7. selector:'my-app',injectables: [NameService]
  8. })
  9. @View({
  10. template:'<h1>Hi {{name}}</h1>' +
  11. '<p>Friends</p>' +
  12. '<ul>' +
  13. ' <li *ng-for="#name of names">{{name}}</li>' +
  14. '</ul>',directives:[NgFor]
  15. })
  16.  
  17. class MyAppComponent
  18. {
  19. name:string;
  20. names:Array<string>;
  21.  
  22. constructor(nameService:NameService)
  23. {
  24. this.name = 'Michal';
  25. this.names = nameService.getNames();
  26. }
  27. }
  28. bootstrap(MyAppComponent);

services / NameService.ts

  1. export class NameService {
  2. names: Array<string>;
  3. constructor() {
  4. this.names = ["Alice","Aarav","Martín","Shannon","Ariana","Kai"];
  5. }
  6. getNames()
  7. {
  8. return this.names;
  9. }
  10. }

所有的时间,我得到错误消息说“没有提供者NameService”…有人可以帮助我发现我的代码的小问题吗?

你必须使用提供程序,而不是注射
  1. @Component({
  2. selector: 'my-app',providers: [NameService]
  3. })

Complete code sample here

猜你在找的Angularjs相关文章