【发布时间】:2019-06-20 06:18:31
【问题描述】:
我想创建一个嵌套路由movies/:id 但是在我当前的配置下,当用户导航到movies/1 时,总是会呈现父组件。我需要MovieDetailComponent 在movies/1 url 上呈现。这是我的配置:
const routes: Routes = [{
path: '',
component: HomeView
},
{
path: 'movies',
component: MoviesView,
children: [{
path: ':id',
component: MovieDetailComponent
}]
},
{
path: 'not-found',
component: PageNotFoundComponent,
pathMatch: 'full'
},
{
path: '**',
redirectTo: 'not-found'
}
];
我尝试先将pathMatch: 'full' 添加到父组件,然后再添加子组件,然后再添加两者。当我将pathMach: 'full' 添加到父组件时,子URL 甚至都不会被命中,当我将pathMatch: 'full' 添加到子组件时,即使URL 是/movies/:id,也只会呈现父组件为什么发生这种情况了吗?
当我将子路径移动到它自己的路径中,而不是嵌套时,组件正确呈现。
const routes: Routes = [{
path: '',
component: HomeView
},
{
path: 'movies',
component: MoviesView
},
{
path: 'movies:id',
component: MovieDetailComponent
} {
path: 'not-found',
component: PageNotFoundComponent,
pathMatch: 'full'
},
{
path: '**',
redirectTo: 'not-found'
}
];
【问题讨论】:
标签: angular routing angular-router