玩弄Angular 2并试图让这个简单的代码工作.但我一直收到错误:
EXCEPTION:无法解析Tab的所有参数(未定义).确保它们都具有有效的类型或注释.
到目前为止,ng2没有将c onstructor(tabs:Tabs) {…注入到构造函数中
这是整个代码:
///<reference path="../../typings/zone.js/zone.js.d.ts"/>
import {Component, Input} from 'angular2/core';
@Component({
selector: 'tab',
template: `
<ul>
<li *ngFor="#tab of tabs" (click)="selectTab(tab)">
{{tab.tabTitle}}
</li>
</ul>
<ng-content></ng-content>
`,
})
export class Tab {
@Input() tabTitle: string;
public active:boolean;
constructor(tabs:Tabs) {
this.active = false;
tabs.addTab(this);
}
}
@Component({
selector: 'tabs',
directives: [Tab],
template: `
<tab tabTitle="Tab 1">
Here's some content.
</tab>
`,
})
export class Tabs {
tabs: Tab[] = [];
selectTab(tab: Tab) {
this.tabs.forEach((myTab) => {
myTab.active = false;
});
tab.active = true;
}
addTab(tab: Tab) {
if (this.tabs.length === 0) {
tab.active = true;
}
this.tabs.push(tab);
}
}
Run Code Online (Sandbox Code Playgroud)
TX
肖恩
那是因为您的Tabs课程是在您的Tab课程和javascript中的课程未被提升之后定义的.
所以你必须用来forwardRef引用一个尚未定义的类.
export class Tab {
@Input() tabTitle: string;
public active:boolean;
constructor(@Inject(forwardRef(() => Tabs)) tabs:Tabs) {
this.active = false;
tabs.addTab(this);
}
}
Run Code Online (Sandbox Code Playgroud)