Angular 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注解有关。 编辑:即使我用另一种类型(例如 AppFrame)替换 SiteLink 参数,我也会得到同样的错误。

我正在使用 alpha 36 和来自 DefinitelyTyped 存储库的最新 d.ts 文件。

可以通过使用前向引用在构造函数中注入 class 本身的实例:

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

有关详细信息,请参阅 here

正如 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;
  }

帮我修好了。