2015-09-06 42 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