簡體   English   中英

角度2:具有多個依賴項的組件

[英]Angular 2: component with more than one dependency

如下代碼:

...
@Component({
  selector: 'sitelink',
  properties: ['route: route, title: title, isHome: home, href: href']
})
@View({
  template: ''
})
export class SiteLink {
  constructor(@Optional() @Host() @SkipSelf() parentFrame: AppFrame, @Optional() @Host() @SkipSelf() parentLink: SiteLink) {
  }
...

從TypeScript轉換為JavaScript的過程中出現以下錯誤

at line 32, file app-frame.ts Argument of type 'typeof SiteLink' is not assignable to parameter of type 'Type'.
Component({
  selector: 'sitelink',
  properties: ['route: route, title: title, isHome: home, href: href']
})
at line 36, file app-frame.ts Argument of type 'typeof SiteLink' is not assignable to parameter of type 'Type'.
View({
  template: ''
})

僅使用一個構造函數參數可以按預期工作。 從參數列表中刪除注釋時,它也不起作用。 刪除@Component@View批注后,代碼將編譯。 因此,該錯誤必須與@Component批注有關。 編輯:即使我將SiteLink參數替換為另一種類型(例如AppFrame),也會出現相同的錯誤。

我正在使用Alpha 36和DefinitelyTyped存儲庫中的最新d.ts文件。

通過使用前向引用,可以在構造函數中注入類本身的實例:

constructor(@Inject(forwardRef(() => SiteLink)) siteLink) {
    this.name = nameService.getName();
}

有關更多詳細信息,請參見此處

正如Jesse Good所寫,這是angular的d.ts文件的問題,請參閱https://github.com/angular/angular/issues/3972

更換

  interface Type extends Function {
     new(args: any): any;
  }

  interface Type extends Function {
     new(...args): any;
  }

為我修復它。

暫無
暫無

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

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