首页 > 解决方案 > 为延迟加载的模块组件指定 routerLink?

问题描述

在这个演示应用程序中,我在延迟加载的书籍模块中有以下路线:

{ path: 'collection', component: CollectionPageComponent },

在搜索组件中,以下路由器链接:

  <a routerLink="collection">Collections</a>

将鼠标悬停在链接上时,它会显示路径books/collection。但是,当单击它时,它会路由到 404 页面。想法?

https://stackblitz.com/edit/angular-temp-slice-demo-phase2?file=src%2Fapp%2Fbooks%2Findex.ts

标签: javascripthtmlangulartypescript

解决方案


路线

{ path: ':id', component: ViewBookComponent, resolve: { book: BookResolverService }}

正在使用您的字符串collection作为id.

为了解决这个问题,

将特定路线放在顶部,即

{ path: 'collection', component: CollectionPageComponent },
{ path: ':id', component: ViewBookComponent, resolve: { book: BookResolverService }}

推荐阅读