Angular2:如何通过更改某些路由参数导航到新网址



>我有一些这样的路由代码:

// routing.module.ts
const routes: Routes = [
  {
    path: ':group',
    component: MainComponent,
    children: [
      { path: 'prod', loadChildren: './prod/prod.module#ProdModule' },
      { path: 'test', loadChildren: './test/test.module#TestModule', },
    ]
  }
];

我期望的效果是这样的导航:本地主机:4200/a/x => 本地主机:4200/b/x"x"可以是"产品"或"测试"。我只想更改参数并保持其他路由不变。

在我的主组件中,我编写了一些代码,但不知道如何进行导航:

// MainComponent
this.activatedRoute.paramMap
  .subscribe((params: ParamMap) => {
    let originalGroup = params.get('group');
    if (originalGroup === 'a') {
      // this.router.navigate();  // <= How to change the group with other routes remaining unchanged ???
    }
  });

这更像是一种黑客攻击,但您可以对其进行编辑以使其更通用。

// MainComponent
this.activatedRoute.paramMap
  .subscribe((params: ParamMap) => {
    let originalGroup = params.get('group');
    if (originalGroup === 'a') {
       // if you are in prod code. (change the prod to test if you are in test code)
       this.router.navigate(['/b','prod']);  
    }
  });

最新更新