Angular2 beta14 Typescript routerLink绑定错误

时间:2016-04-15 19:28:29

标签: asp.net typescript angular

我正在尝试使用Angular2的路由器创建向导。 Angular2建议有一个主引导文件,它将引导所有应用程序组件。由于我无法进行SPA,我希望每个页面都能引导它自己的组件。

运行应用程序时,我收到以下关于routerLink的错误:

“EXCEPTION:模板解析错误: 无法绑定到'routerLink',因为它不是已知的本机属性(“For =”#向导的向导“[selected] =”SelectedWizard == wizard“[value] =”wizard.name“[ERROR - &gt ;] [routerLink] =“''+ wizard.path +''”> {{wizard.name}}         “

这是我的页面组件:

/// <reference path="angular2/typings/browser.d.ts" />
/// <reference path="angular2/core.d.ts" />
/// <reference path="angular2/router.d.ts" />

import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';
import {ROUTER_PROVIDERS} from 'angular2/router';

import {Wizard1} from './wizard1';
import {Wizard2} from './wizard2';
import {Step1} from './step1';
import {Step2} from './step2';
import {Step3} from './step3';
import {Step4} from './step4';
import {Step5} from './step5';

@Component({
    selector: 'page',
    template:
'<div>
    <select (change)="setSelectedWizard($event.target.value)">
        <option *ngFor="#wizard of Wizards" [selected]="SelectedWizard == wizard" [value]="wizard.name" [routerLink]="'' + wizard.path + ''">{{wizard.name}}</option>
    </select>
    <wizard1>
        <step1>
            <h1>First!</h1>
        </step1>
        <step2>
            <h1>Second!</h1>
        </step2>
        <step3>
            <h1>Third!</h1>
        </step3>
        <step4>
            <h1>Fourth!</h1>
        </step4>
        <nav>
            <button type="button" value="Next" [class]="(SelectedStepIndex == Steps.length) ? 'btn btn-primary hidden' : 'btn btn-default'" (click)="next()" [routerLink]="['' + Steps[SelectedStepIndex] + '']">Next</button>
        </nav>
        <router-outlet></router-outlet>
    </wizard1>
    <wizard2>
        <step1>
            <h1>First!</h1>
        </step1>
        <step5>
            <h1>Fifth!</h1>
        </step5>
        <nav>
            <button type="button" value="Next" [class]="(SelectedStepIndex == Steps.length) ? 'btn btn-primary hidden' : 'btn btn-default'" (click)="next()" [routerLink]="['' + Steps[SelectedStepIndex] + '']">Next</button>
        </nav>
        <router-outlet></router-outlet>
    </wizard2>
    <router-outlet></router-outlet>
</div>'
})
export class Page {
    Wizards = [{name: 'wizard1', path:'/wizard1'}, {name: 'wizard2', path: '/wizard2'}];
    SelectedWizard = this.Wizards[0];

    setSelectedWizard(value) {
       this.SelectedWizard = value;
    }
}

bootstrap(Page, [ROUTER_PROVIDERS]);
bootstrap(Wizard1, [ROUTER_PROVIDERS]);
bootstrap(Wizard2, [ROUTER_PROVIDERS]);
bootstrap(Step1);
bootstrap(Step2);
bootstrap(Step3);
bootstrap(Step4);
bootstrap(Step5);

Wizard1

/// <reference path="angular2/core.d.ts" />
/// <reference path="angular2/router.d.ts" />

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_PROVIDERS} from 'angular2/router';

@RouteConfig([
    { path: '/wizard1', name: 'wizard1', component: Wizard1 }
])
@Component({
    selector: 'wizard1',
    template:`<div><ng-content></ng-content></div>`
})
export class Wizard1 {
    Steps = ['/Step1', '/Step2', '/Step3', '/Step4'];
    SelectedStepIndex = 0;

    next() {
        ++this.SelectedStepIndex;
    }
}

Wizard2

/// <reference path="angular2/core.d.ts" />
/// <reference path="angular2/router.d.ts" />

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_PROVIDERS} from 'angular2/router';

@RouteConfig([
    { path: '/wizard2', name: 'wizard2', component: Wizard2 }
])
@Component({
    selector: 'wizard2',
    template:`<div><ng-content></ng-content></div>`
})
export class Wizard2 {
    Steps = ['/Step1', '/Step5'];
    SelectedStepIndex = 0;

    next() {
        ++this.SelectedStepIndex;
    }
}

所有步骤都与他们自己的.ts文件类似

/// <reference path="angular2/core.d.ts" />
/// <reference path="angular2/router.d.ts" />

import {Component} from 'angular2/core';
import {RouteConfig} from 'angular2/router';

@RouteConfig([
    { path: '/Step1', name: 'Step1', component: Step1 }
])
@Component({
    selector: 'step1',
    template: `<div><ng-content></ng-content></div>`
})
export class Step1 {

}

发生了什么事?为什么这不起作用?

环境

  1. Visual Studio 2015更新1
  2. ASP.NET 5和MVC 6
  3. DNX 4.5.1和5.0
  4. Angular2 Typescript

2 个答案:

答案 0 :(得分:2)

您遗失了ROUTER_DIRECTIVES,导致Page组件&amp; ComponentMetaData的指令选项,这就是为什么角度不理解routerLink&amp;页面上有routerOutlet指令。请包含以下行

import {ROUTER_PROVIDERS, ROUTER_DIRECTIVES} from 'angular2/router';

然后明显在该组件ROUTER_DIRECTIVES选项

中注入directives
@Component({
    selector: 'page',
    //....
    //other options
    //....
    directives: [ROUTER_DIRECTIVES]
})

我不确定您为什么多次bootstrap申请?理想情况下,应该只有一个main-component,其他组件将是它的子项。

答案 1 :(得分:1)

ROUTER_DIRECTIVES添加到您的组件提供商:

@Component({directives: [ROUTER_DIRECTIVES]})