2017-04-13 97 views
1

在页面中路由未发生。我已经使用了HTML和配置代码。routerLink未在角度2中工作

<nav aria-label="Page navigation"> 
    <ul class="pagination pagination-plain"> 
     <li> 
     <a href="review/words" aria-label="Previous"> 
      <span aria-hidden="true">&laquo;</span> 
     </a> 
     </li> 
     <li [routerLinkActive]="['active']"> 
     <a [routerLink]="review/words">1</a> 
     </li> 
     <li [routerLinkActive]="['active']"> 
     <a [routerLink]="review/phrase">2</a> 
     </li> 
     <li> 
     <a href="javascript:void(0)" aria-label="Next"> 
      <span aria-hidden="true">&raquo;</span> 
     </a> 
     </li> 
    </ul> 
    </nav> 

路由配置代码

const appRoutes: Routes = [ 
    { 
    path: '', 
    component: IndexComponent, 
}, 
{ 
    path: 'index', 
    component: IndexComponent, 
}, 
{ 
    path: 'review/words', 
    component: WordsComponent, 
}, 
{ 
    path: 'review/phrase', 
    component: SentenceComponent, 
} 
]; 

我收到的时候我在分页链接点击“1”或“2”下面的错误。

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'review/phrase/NaN' Error: Cannot match any routes. URL Segment: 'review/phrase/NaN' at ApplyRedirects.noMatchError (router.es5.js:1404) [angular] at CatchSubscriber.selector (router.es5.js:1379) [angular] at CatchSubscriber.error (catch.js:104) [angular] at MapSubscriber.Subscriber._error (Subscriber.js:128) [angular] at MapSubscriber.Subscriber.error (Subscriber.js:102) [angular] at MapSubscriber.Subscriber._error (Subscriber.js:128) [angular] at MapSubscriber.Subscriber.error (Subscriber.js:102) [angular] at MapSubscriber.Subscriber._error (Subscriber.js:128) [angular] at MapSubscriber.Subscriber.error (Subscriber.js:102) [angular]

回答

3

尝试类似这样的东西。

<a [routerLink]="['review/words']">1</a> 

这是它期望的输入。 !

编辑: 你也没有路由器容器在你的HTML。 在模板文件的末尾添加此项。

<router-outlet></router-outlet> 
+0

@Partha Ghiya:谢谢你的回答。它路由到URL。但是,除非我刷新它,否则页面不会被加载。你能帮忙吗? –

+0

@GreenComputers导致你没有router-outlet。 添加此结果在您的HTML。 '' –

+0

@Partha Ghiya:已添加。但同样的问题。请帮忙。 –

2

我怀疑你的问题是你没有将RouterModule(这是声明RouterLink的地方)导入到使用此模板的模块中。

我有一个类似的问题,我花了一些时间来解决,因为这一步没有在文档中提到。

,所以还是与此模板声明部件内置模块,并添加:

import { RouterModule } from '@angular/router'; 

然后将其添加到您的模块,例如进口

@NgModule({ 
    imports: [ 
    CommonModule, 
    RouterModule 
    ], 
    declarations: [MyTemplatesComponent] 
}) 
export class MyTemplatesModule { }