角2错误: [英] Angular 2 error:
问题描述
与2角玩弄,并试图让这个简单的code工作。
但我不断收到一个错误信息:
Playing around with Angular 2 and trying to get this simple code working. yet I keep getting an error of:
例外:无法解析所有的参数选项卡(不确定)。确保
它们都具有有效的类型或注释。
EXCEPTION: Cannot resolve all parameters for Tab(undefined). Make sure they all have valid type or annotations.
据NG2在c未注入 onstructor(标签:标签){...
到构造
As far ng2 is not injecting in constructor(tabs:Tabs) {…
into the constructor
下面是整个code:
///<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);
}
}
TX
肖恩
推荐答案
这是因为你的标签
类的<code>设置页$ C之后定义$ C>类和类在JavaScript中没有悬挂一>
That's because your Tabs
class is defined after your Tab
class and classes in javascript aren't hoisted.
所以,你必须使用<一个href=\"https://angular.io/docs/ts/latest/api/core/forwardRef-function.html\"><$c$c>forwardRef$c$c>引用一个尚未定义的类。
So you have to use forwardRef
to reference a not yet defined class.
export class Tab {
@Input() tabTitle: string;
public active:boolean;
constructor(@Inject(forwardRef(() => Tabs)) tabs:Tabs) {
this.active = false;
tabs.addTab(this);
}
}
这篇关于角2错误:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!