更新了 Angular2 中 @Parent() 的语法

Updated Syntax for @Parent() in Angular2

我正在查看 angular2 的示例,其中 class 构造函数引用 @Parent()。我收到一个错误:(58, 18) TS2304:找不到名称 'Parent'。我看到另一个回答的问题说使用 'Host' 但我得到了同样的错误。我正在使用 Alpha.45。 Here 是对示例的引用。完整代码在最后。

constructor(@Parent() tabs:Tabs) {
        tabs.addTab(this);
        this._active=true ;
    }

@Host parameter decorator just restrict the dependency resolution with the host component. You don't have to use it to get the closest component of specified Type. However, you may need to use @Inject with forwardRef 如果您的依赖项尚未定义。

例如(参见this plunk,我在那里不使用forwardRef):

import {Component, Input, Inject, forwardRef} from 'angular2/angular2'

@Component({
  selector: 'tab',
  template: 'Tab {{ index }}'
})
class Tab {
  @Input() index: number;

  constructor(@Inject(forwardRef(() => Tabs)) tabs: Tabs) {
    console.log(tabs);
  }
}

@Component({
  selector: 'tabs',
  template: 'Tabs (<ng-content></ng-content>)'
})
class Tabs {}

@Component({
  selector: 'my-app',
  directives: [Tabs, Tab],
  template: `
    <tabs>
      <tab index="1"></tab>
      <tab index="2"></tab>
    </tabs>
  `
})
export class App {}