我正在努力尋找我的上班路線。我使用的 RouterLink 沒有導航到路線。我的 app.routing 有
const routes: Routes = [
{
path: '',
redirectTo: '/',
pathMatch: 'full'
},
{
path: '',
component: LayoutComponent,
loadChildren: () => import('./views/main/main.module').then( m => m.MainModule )
}
];
mainModule 有以下內容
const routes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'search',
component: SearchFormComponent,
loadChildren: () => import('../../views/search/search.module').then( m => m.SearchModule )
},
{
path: '**',
redirectTo: '/home',
pathMatch: 'full'
}
];
mainModule 宣告并匯出 LayoutComponent
在 LayoutComponent.html 中
<a [routerLink]="'/search'">
Search
</a>
但是當我在 UI 中單擊搜索時,路由不會轉到搜索組件。它停留在 Home 組件上。請幫忙 - 我哪里出錯了?
uj5u.com熱心網友回復:
交換路線的順序。
const routes: Routes = [
{
path: 'search',
component: SearchFormComponent,
loadChildren: () => import('../../views/search/search.module').then( m => m.SearchModule )
},
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: '**',
redirectTo: '/home',
pathMatch: 'full'
}
];
Angular 路由器以先到先得的方式作業。
配置中路由的順序很重要,這是設計使然。路由器在匹配路由時使用先匹配獲勝策略,因此更具體的路由應該放在不太具體的路由之上。在上面的配置中,首先列出具有靜態路徑的路由,然后是與默認路由匹配的空路徑路由。通配符路由排在最后,因為它匹配每個 URL,并且只有在沒有其他路由首先匹配時才應該選擇它。
從這里的角度網站
正如這里所解釋的
更新:雖然它仍然是一個問題,所以我會在這里留下原始答案。嘗試將您的路由器鏈接設定為
<a [routerLink]="['search']">
Search
</a>
uj5u.com熱心網友回復:
最后通過洗掉 '' 和 ** 并將它們替換為
{
path: '',
component: HomeComponent,
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314759.html
標籤:有角的
