Angular 2:有多个依赖的组件

Angular 2: component with more than one dependency

本文关键字:依赖 组件 Angular      更新时间:2023-09-26

以下代码:

...
@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文件。

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

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