为什么路由不起作用?

时间:2016-02-19 18:14:33

标签: routing angular

我试图通过angular2中的新路由来执行向导

我的app.ts

import {Component,ViewEncapsulation} from 'angular2/core';
import {RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router';
import {WizardCmp} from './wizard';
@Component({
  selector: 'my-app',
  providers: [],
  encapsulation: ViewEncapsulation.None,
   template: `
     <p>header</p>
     <router-outlet></router-outlet>
     `,
    directives: [ROUTER_DIRECTIVES]
  })
    @RouteConfig([
     { path: '/...', component:WizardCmp, as: 'Home',useAsDefault:true},
       ])
      export class App {}

我这里只有一条路线,带有三个点符号。

wizard.ts

import {Component, View} from 'angular2/core';
import {Router,RouteConfig,ROUTER_DIRECTIVES} from 'angular2/router';
import {FirstFormCmp} from './form1';
import {SecondFormCmp} from './form2';
import {ThirdFormCmp} from './form3';
 @Component({
     selector: 'wizard'
         })
 @View({

   template: `
  <h1>wizard</h1>
  <router-outlet></router-outlet>
         `,
  directives: [...ROUTER_DIRECTIVES]
    })
  @RouteConfig([
  {path: 'first', name: 'FirstForm', component: FirstFormCmp, useAsDefault:   true},
  {path: 'second', name: 'SecondForm', component: SecondFormCmp},
  {path: 'third', name: 'ThirdForm', component: ThirdFormCmp},


 export class WizardCmp {}

但它没有显示任何内容。在控制台中没有错误。   这是Plunker

1 个答案:

答案 0 :(得分:3)

正在进行小修改http://plnkr.co/edit/bppL6TZDbRBD3mVIjw3f?p=preview ...

使用plunker,您必须使用HashLocationStrategy。也许因为应用程序在<iframe>中运行,所以不确切知道原因。但这不是Angular2或Router问题,而是限制了plunker。

我刚刚将HashLocationStrategy添加到bootstrap:

bootstrap(App, [
  ROUTER_PROVIDERS,
  provide(APP_BASE_HREF, {useValue : '/'}),
  provide(LocationStrategy, {useClass: HashLocationStrategy})
])

指向模板的链接:

<a [routerLink]="['/Wizard', 'FirstForm']">first</a>
<a [routerLink]="['/Wizard', 'SecondForm']">second</a>
<a [routerLink]="['/Wizard', 'ThirdForm']">third</a>

其余代码未更改且有效......