简体   繁体   中英

routerLink is not working in angular 2

Routing in page is not happening. I have used below HTML and config code.

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

routing config code is

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

I am getting the below error when i click on '1' or '2' in my pagination link.

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]

Try something like this.

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

This is how it expects input. !

Edit: Also you dont have router container in your html. Add this at end of template file.

<router-outlet></router-outlet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM