2015-09-06 31 views
0

下面的代碼組件:角2:擁有多一個依賴

... 
@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) { 
    } 
... 

給我下面的錯誤從打字稿爲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註釋相關。 編輯:即使我用另一種類型(例如AppFrame)替換SiteLink參數,我也會得到相同的錯誤。

我使用的是來自DefinitelyTyped存儲庫的alpha 36和最新的d.ts文件。

+0

我不確定你想要做什麼是可能的。在「SiteLink」的構造函數中,你正在調用這個類本身。你確定是你想要的嗎? –

+0

是的,我想獲得父級「SiteLink」組件。 – derjasper

+1

錯誤可能與此[問題]相同(https://github.com/angular/angular/issues/3972)。你使用什麼版本的打字稿? –

回答

0

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

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

更換

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

固定對我來說。

0

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

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

詳情請參閱here

+0

這並不能解決問題。即使我用另一種類型替換SiteLink參數,我也會得到相同的錯誤。 – derjasper