Angular 9 路径子在导航上不起作用

问题描述 投票:0回答:2

我有一个父路径的子路径,但当我调用它时,它将url改为正确的路径,但加载ProductDetailsComponent而不是ChangeEmailComponent。

app-routing.ts

 const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full'},
  { path: 'home', component: HomeComponent },
  { path:':productName/:productId', component: ProductDetailsComponent },
  { path:'perfil', component: ProfileComponent, children: [
    { path: 'email', component: ChangeEmailComponent },
  ]},
  { path: '404', component: NotFoundComponent },
  { path: '**', redirectTo: '/404'}
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules, enableTracing: false })
  ],
  exports: [RouterModule]
})

ProfileComponent.ts

this.router.navigate(['email'], { relativeTo:this.route });

将url改为fileemail,但打开ProductDetailsComponent而不是ChangeEmailComponent。

angular angular-ui-router
2个回答
1
投票

在profilecomponent里面放一个路由器出口,或者把你的路由改为:-。

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full'},
  { path: 'home', component: HomeComponent },
  { path:':productName/:productId', component: ProductDetailsComponent },
  { path:'perfil', children: [
    { path: '', component: ProfileComponent},
    { path: 'email', component: ChangeEmailComponent },
  ]},
  { path: '404', component: NotFoundComponent },
  { path: '**', redirectTo: '/404'}
];

解释请参考:- https:/medium.com@aakashgarg19the-of-nested-router-outlets-in-angular-dafb38245a30。


1
投票

试着给你的产品路线加个前缀。

{ path:':productName/:productId', component: ProductDetailsComponent },

{ path:'product/:productName/:productId', component: ProductDetailsComponent },

0
投票

解决方案如上所述,必须将两者结合起来。

  const routes: Routes = [
      { path: '', redirectTo: '/home', pathMatch: 'full'},
      { path: 'home', component: HomeComponent },
      { path:'product/:productName/:productId', component: ProductDetailsComponent },
      { path:'perfil', children: [
        { path: '', component: ProfileComponent},
        { path: 'email', component: ChangeEmailComponent },
      ]},
      { path: '404', component: NotFoundComponent },
      { path: '**', redirectTo: '/404'}
];
© www.soinside.com 2019 - 2024. All rights reserved.