重定向到另一个路径,而不是以角度4给出

时间:2018-02-08 13:46:39

标签: angular angular4-router

应用路线:

[
    { path: '', loadChildren: './layout/layout.module#LayoutModule', canActivate: [AuthGuard] },
    { path: 'login', loadChildren: './login/login.module#LoginModule' },
    { path: 'dashboard', loadChildren: './layout/layout.module#LayoutModule' },
    { path: 'error', loadChildren: './server-error/server-error.module#ServerErrorModule' },
    { path: 'access-denied', loadChildren: './access-denied/access-denied.module#AccessDeniedModule' },
    { path: 'not-found', loadChildren: './not-found/not-found.module#NotFoundModule' },
    { path: '**', redirectTo: 'not-found' }
]

LayoutModule路线:

   [{
        path: '',
        component: LayoutComponent,
        children: [
            { path: '', redirectTo: 'dashboard' },
            { path: 'allcookies' , loadChildren: './allcookies/allcookies.module#AllcookiesModule' },
            { path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' },
            { path: 'details', loadChildren: './details/details.module#DetailsModule' },
            { path: 'domains', loadChildren: './domains/domains.module#DomainsModule' },
        ]
    }]

我有像这样的DetailsModule路由配置,

[
  { path : '' , redirectTo: 'email-details' },
  { path : 'email-details' , component : EmailDetailsComponent },
  { path : 'email-details/:email_id' , component : EmailDetailsComponent },
  { path : 'email-details/:email_id/:curr_page' , component : EmailDetailsComponent },
  { path : 'fingerprint-details/:fp_id/:fp_page/domains/:user_id/:domain_page' , component : FingerprintDetailsComponent },
  { path : 'fingerprint-details/:fp_id/:fp_page/domains/:user_id' , component : FingerprintDetailsComponent },
  { path : 'fingerprint-details/:fp_id/:fp_page' , component : FingerprintDetailsComponent },
  { path : 'fingerprint-details/:fp_id' , component : FingerprintDetailsComponent },
  { path : 'fingerprint-details' , component : FingerprintDetailsComponent }
]

我已经给了网址

  

http://localhost:4302/details/fingerprint-details

但它重定向到

  

http://localhost:4302/details/email-details/fingerprint-details

2 个答案:

答案 0 :(得分:0)

在redirectTo属性中,您应该从路由级别编写绝对路径。 我也总是在我的所有路线中使用pathMatch: 'full',以确保我不会涉及相对路径。

使用该配置进行测试:

[
  { path : '' , redirectTo: '/email-details', pathMatch: 'full'},
  { path : 'email-details' , pathMatch: 'full', component : EmailDetailsComponent },
  { path : 'email-details/:email_id' , pathMatch: 'full', component : EmailDetailsComponent },
  { path : 'email-details/:email_id/:curr_page' , pathMatch: 'full', component : EmailDetailsComponent },
  { path : 'fingerprint-details/:fp_id/:fp_page/domains/:user_id/:domain_page' , pathMatch: 'full', component : FingerprintDetailsComponent },
  { path : 'fingerprint-details/:fp_id/:fp_page/domains/:user_id' , pathMatch: 'full', component : FingerprintDetailsComponent },
  { path : 'fingerprint-details/:fp_id/:fp_page' , pathMatch: 'full', component : FingerprintDetailsComponent },
  { path : 'fingerprint-details/:fp_id', pathMatch: 'full', component : FingerprintDetailsComponent },
  { path : 'fingerprint-details' , pathMatch: 'full', component : FingerprintDetailsComponent }
]

以下是doc的相应链接: https://angular.io/guide/router#redirecting-routes

答案 1 :(得分:0)

昆汀打败了我。

https://angular.io/guide/router

  

"重定向路由需要pathMatch属性来告诉路由器如何   将URL匹配到路径的路径。如果路由器抛出错误   你没有。"