在我的組件中,我有菜單項列表。一個包含其他頁面。角2:使用不同類型的段塞路由
menu.component.ts
selectedPost(postslug){
console.log('post slg ', postslug);
this._router.navigate([postslug]);
}
selectedPage(pageslug){
console.log('page slg ', pageslug);
this._router.navigate([pageslug]);
}
app.routing.module.ts
const routes: Routes = [
{
path: '',
component: PostListComponent,
pathMatch: 'full'
}
,
{
path: ':postslug', // This is for the posts
component: PostSingleComponent,
}
,
{
path: ':pageslug',
component: PageSingleComponent // This is for the pages
}
];
正如我理解的是,因爲我通過它應該拿起並加載正確的組件定製蛞蝓。但它加載了第一個具有':+'的東西。
有沒有辦法解決這個問題?
如果沒有辦法通過函數推送自定義類別,並且在路由中重定向到類別?
例如,在功能:
selectedPost(catName, postslug){
console.log('post slg ', postslug);
this._router.navigate([catName, postslug]);
}
而在路由我會:
const routes: Routes = [
{
path: '',
component: PostListComponent,
pathMatch: 'full'
}
,
{
path: 'catName/:postslug', // This is for the posts
component: PostSingleComponent,
}
,
{
path: ':pageslug',
component: PageSingleComponent // This is for the pages
}
];
把自己放在路由器的鞋子裏。您被要求導航到/ 1234。你怎麼可能知道1234是帖子還是頁面?如何使用/ posts/1234和/ pages/1234? –
這是我在另一個例子中嘗試的,但是也沒有工作。因爲我不知道如何傳遞自定義類別。 –
如果catName變量的值是'posts'或者'pages',那麼你需要路徑爲'posts /:postslug',另一個爲'pages /:pageslug'。 –