我如何使用 angular 路由重定向用户
how I can redirect user with angular routing
当我尝试转到 myaddress/home 时出现错误。当用户在地址行 myaddress/home 中输入时,需要将他重定向到 myaddress/home/allQuestions。另一条路线有效。我用 angular 8.
const routes: Routes = [
{
path: '', component: MainLayoutComponent, children: [
{path: '', redirectTo: '/login', pathMatch: 'full'},
{path: 'login', component: LoginFormComponent},
{path: 'registration', component: RegistrationFormComponent}
]
},
{ path: 'home', component: HomeLayoutComponent, children: [
{path: '', redirectTo: '/allQuestions', pathMatch: 'full'},
{path: 'allQuestions', component: AllQuestionsComponent},
{path: 'addQuestion', component: AddQuestionComponent},
], canActivate: [AuthenticationGuard]
}
];
错误:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'allQuestions'
Error: Cannot match any routes. URL Segment: 'allQuestions'
现在使用您的代码 angular 路由器,将尝试用 /login 替换空洞路由地址 (/home)。
要将地址设置为redirectTo,不能使用/
更改第二行
...
{ path: 'home', component: HomeLayoutComponent, children: [
{path: '', redirectTo: '/allQuestions', pathMatch: 'full'},
...
至此
{path: '', redirectTo: 'allQuestions', pathMatch: 'full'},
错误修复代码
const routes: Routes = [
{
path: '', component: MainLayoutComponent, children: [
{path: '', redirectTo: 'login', pathMatch: 'full'},
{path: 'login', component: LoginFormComponent},
{path: 'registration', component: RegistrationFormComponent}
]
},
{ path: 'home', component: HomeLayoutComponent, children: [
{path: '', redirectTo: 'allQuestions', pathMatch: 'full'},
{path: 'allQuestions', component: AllQuestionsComponent},
{path: 'addQuestion', component: AddQuestionComponent},
], canActivate: [AuthenticationGuard]
}
];
下一步
修复此代码后,请确保更改加载组件的方式并考虑使用延迟加载。
我的意思是,例如,出于授权目的,您可以拥有 AuthModule,该模块可以拥有自己的 auth-router.module.ts,其中包含 auth 模块的路由信息(如 /signin 和 /signup)。这样,只有当去那里的路线称为 !
时,模块才会加载
当我尝试转到 myaddress/home 时出现错误。当用户在地址行 myaddress/home 中输入时,需要将他重定向到 myaddress/home/allQuestions。另一条路线有效。我用 angular 8.
const routes: Routes = [ { path: '', component: MainLayoutComponent, children: [ {path: '', redirectTo: '/login', pathMatch: 'full'}, {path: 'login', component: LoginFormComponent}, {path: 'registration', component: RegistrationFormComponent} ] }, { path: 'home', component: HomeLayoutComponent, children: [ {path: '', redirectTo: '/allQuestions', pathMatch: 'full'}, {path: 'allQuestions', component: AllQuestionsComponent}, {path: 'addQuestion', component: AddQuestionComponent}, ], canActivate: [AuthenticationGuard] } ];
错误:
ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'allQuestions' Error: Cannot match any routes. URL Segment: 'allQuestions'
现在使用您的代码 angular 路由器,将尝试用 /login 替换空洞路由地址 (/home)。
要将地址设置为redirectTo,不能使用/
更改第二行
...
{ path: 'home', component: HomeLayoutComponent, children: [
{path: '', redirectTo: '/allQuestions', pathMatch: 'full'},
...
至此
{path: '', redirectTo: 'allQuestions', pathMatch: 'full'},
错误修复代码
const routes: Routes = [
{
path: '', component: MainLayoutComponent, children: [
{path: '', redirectTo: 'login', pathMatch: 'full'},
{path: 'login', component: LoginFormComponent},
{path: 'registration', component: RegistrationFormComponent}
]
},
{ path: 'home', component: HomeLayoutComponent, children: [
{path: '', redirectTo: 'allQuestions', pathMatch: 'full'},
{path: 'allQuestions', component: AllQuestionsComponent},
{path: 'addQuestion', component: AddQuestionComponent},
], canActivate: [AuthenticationGuard]
}
];
下一步
修复此代码后,请确保更改加载组件的方式并考虑使用延迟加载。
我的意思是,例如,出于授权目的,您可以拥有 AuthModule,该模块可以拥有自己的 auth-router.module.ts,其中包含 auth 模块的路由信息(如 /signin 和 /signup)。这样,只有当去那里的路线称为 !
时,模块才会加载