【问题标题】:how I can redirect user with angular routing我如何使用角度路由重定向用户
【发布时间】:2020-05-11 22:55:52
【问题描述】:

当我尝试转到 myaddress/home 时,我收到了错误消息。当用户输入地址行 myaddress/home 时,需要将他重定向到 myaddress/home/allQuestions。另一条路线有效。我使用角度 8。

常量路线:路线= [ { 路径:'',组件:MainLayoutComponent,孩子:[ {path: '', redirectTo: '/login', pathMatch: 'full'}, {路径:'登录',组件:LoginFormComponent}, {路径:'注册',组件:RegistrationFormComponent} ] }, { 路径:'home',组件:HomeLayoutComponent,孩子:[ {path: '', redirectTo: '/allQuestions', pathMatch: 'full'}, {路径:'allQuestions',组件:AllQuestionsComponent}, {路径:'addQuestion',组件:AddQuestionComponent}, ], canActivate: [AuthenticationGuard] } ];

错误:

ERROR 错误:未捕获(在承诺中):错误:无法匹配任何路由。 URL 段:'allQuestions'
错误:无法匹配任何路由。 URL 段:'allQuestions'

【问题讨论】:

    标签: angular-routing angular8


    【解决方案1】:

    现在使用您的代码,角度路由器,将尝试用 /login 替换漏洞路由地址 (/home)。

    要将地址设置为redirectTo,不能使用/

    改变第二行

    ...
    { path: 'home', component: HomeLayoutComponent, children: [
              {path: '', redirectTo: '/allQuestions', pathMatch: 'full'},
    ...
    

    到这里

    {path: '', redirectTo: 'allQuestions', pathMatch: 'full'},
    

    Bug 修复代码

    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 )。这样,只有当去那里的路由调用时,模块才会加载!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2013-08-10
      相关资源
      最近更新 更多