簡體   English   中英

錯誤:ES5 Angular2中沒有提供服務的提供程序

[英]Error : No provider for service in ES5 Angular2

我已經開始從angular.io學習Angular2 我從本教程中舉了一個例子。

被創造的

零件 :

function FriendService (){
  this.friendList = ['Wam','Pam','Sam','Cam'];
}

function PersonalInfo (friends){
  this.myName="Jam";

}
PersonalInfo.annotations = [
  new angular.ComponentAnnotation({
    selector : 'app',
    injectables :[FriendService]
  }),
  new angular.ViewAnnotation({
    template:'<h3>{{myName}} Friend\'s</h3>'
  }) 
]; 
PersonalInfo.parameters =[[FriendService]];
document.addEventListener('DOMContentLoaded', function(){
  angular.bootstrap(PersonalInfo);
})

查看:

  <body>
    <app></app>
  </body>

在這里,我已經將FriendService注入組件,並且還將參數傳遞給PersonalInfo組件。 但是它給出了錯誤:

沒有FriendService的提供者! (個人信息-> FriendService)

有人對此錯誤有任何想法嗎?

2016年1月14日更新-angular2@^2.0.0-beta.1

這是新語法。

function FriendService(){
  this.friendList = ['Wam','Pam','Sam','Cam'];
}

var PersonalInfo = ng.core
  .Component({
    selector: 'app',
    providers: [FriendService]
  })
  .View({
    template:'<h3>{{myName}} Friend\'s</h3>'
  })
  .Class({
    constructor: [FriendService, function(friends){
      this.myName="Jam";
    }]
  })

ng.platform.browser.bootstrap(PersonalInfo)

從Angular 2 alpha-25 +起, Component.injectables成為Component.appInjector

另外,我不確定參數是否需要雙方括號。 雖然我可能是錯的。 您可能要考慮使用A庫在Angular中編寫ES5注釋。

嘗試以下更改:

PersonalInfo.annotations = [
  new angular.ComponentAnnotation({
    selector : 'app',
    appInjector :[FriendService]    // injectables -> appInjector
  }),
  new angular.ViewAnnotation({
    template:'<h3>{{myName}} Friend\'s</h3>'
  }) 
]; 
PersonalInfo.parameters =[FriendService];  // [[]] -> []

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM