为什么这个路由配置不起作用?路由器正在尝试访问
/posts
,但不应访问 /posts
。目前它显示一个空白页面。 BlankComponentComponent
包含路由器出口,FullComponentComponent
包含页眉、路由器出口和页脚。
当用户登录时,应使用
BlankComponentComponent
路由器插座,否则应使用FullComponentComponent
。我记得我在 Angular 14 中使用过它。
export const routes: Routes = [
{
path: '',
component: BlankComponentComponent,
canActivate: [
() => {
return true;
},
],
children: [
{
path: '',
redirectTo: '/auth',
pathMatch: 'full',
},
{
path: 'auth',
children: [...authRoutes],
},
],
},
{
path: '',
component: FullComponentComponent,
canActivate: [
() => {
return false;
},
],
children: [
{
path: '',
redirectTo: 'posts',
pathMatch: 'full',
},
{
path: 'posts',
loadComponent: () =>
import('./post/posts/posts.component').then((c) => c.PostsComponent),
},
],
},
];
can activate 函数返回 false,因此可能无法为帖子加载该组件。
canActivate: [
() => {
return true; // <- change this!
},
],