routerLink 在 angular 2 中不工作
routerLink is not working in angular 2
页面中的路由没有发生。我在下面使用了 HTML 和配置代码。
<nav aria-label="Page navigation">
<ul class="pagination pagination-plain">
<li>
<a href="review/words" aria-label="Previous">
<span aria-hidden="true">«</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">»</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”时出现以下错误 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]
尝试这样的事情。
<a [routerLink]="['review/words']">1</a>
这是它期望输入的方式。 !
编辑:
此外,您的 html 中没有路由器容器。
将其添加到模板文件的末尾。
<router-outlet></router-outlet>
页面中的路由没有发生。我在下面使用了 HTML 和配置代码。
<nav aria-label="Page navigation">
<ul class="pagination pagination-plain">
<li>
<a href="review/words" aria-label="Previous">
<span aria-hidden="true">«</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">»</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”时出现以下错误 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]
尝试这样的事情。
<a [routerLink]="['review/words']">1</a>
这是它期望输入的方式。 !
编辑: 此外,您的 html 中没有路由器容器。 将其添加到模板文件的末尾。
<router-outlet></router-outlet>