2016-04-15 77 views
1

我試圖用Angular2的路由器創建一個嚮導。 Angular2建議有一個主引導文件,它將引導所有的應用程序組件。由於我無法做SPA,我希望每個頁面都能引導它自己的組件。Angular2 beta14 Typescript routerLink綁定錯誤

我得到routerLink以下錯誤,當我運行應用程序:

「EXCEPTION:模板解析錯誤: 不能綁定到‘routerLink’,因爲它不是一個已知的原生屬性(」對於= 「#Wizard of Wizards」[selected] =「SelectedWizard == wizard」[value] =「wizard.name」[ERROR - >] [routerLink] =「''+ wizard.path +''」> {{wizard。名}} 「

這是我的頁面組件:

/// <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和6 MVC
  3. DNX 4.5.1和5.0
  4. Angular2打字稿

回答

1

添加ROUTER_DIRECTIVES到您的組件供應商:

@Component({directives: [ROUTER_DIRECTIVES]}) 
2

你是缺少ROUTER_DIRECTIVES缺少導入Page組件&指令選項ComponentMetaData,這就是角度不理解的原因routerLink & routerOutlet指令在頁面上。請包括下列線

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

那麼很明顯在注入該組件directives選項

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

我不知道你爲什麼這樣做bootstrap您的應用程序多次ROUTER_DIRECTIVES?理想情況下,應該只有一個main-component,其他組件將是它的孩子。

+0

它不是一個單一的網頁應用程序。你會有一個像你在SPA中所說的根。在我的情況下,每個頁面都是根組件。 – RoninCoder

+0

@Hani擁有'main-component'不是'SPA'特定的東西..你可能在這種情況下也有單一的應用程序根目錄,不是嗎?那你得到的錯誤呢?這解決了嗎? –

+0

我的頁面是根。是的,我可以將bootstrapping放在一個單獨的文件中,但是之後每個頁面都需要這樣的文件。在SPA中,你會有一個這樣的引導。 – RoninCoder