角2错误: [英] Angular 2 error:

查看:121
本文介绍了角2错误:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与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>设置页类和类在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引用一个尚未定义的类。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆