How do I redirect all the routes from one module to another keeping the path segements and query params and states?

Suppose the following route configs:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const appRoutes: Routes = [
{
path: "home",
canMatch: [platformGuard],
canActivate: [authenticationGuard],
resolve: {userConfig: userConfigResolver},
loadChildren: () => import("@netway/home/home.module").then(value => value.HomeModule),
},
{
path: "pwa",
canMatch: [platformGuard],
canActivate: [authenticationGuard],
resolve: {userConfig: userConfigResolver},
loadChildren: () => import("@netway/pwa/pwa.module").then(value => value.PwaModule),
},
];
</code>
<code>const appRoutes: Routes = [ { path: "home", canMatch: [platformGuard], canActivate: [authenticationGuard], resolve: {userConfig: userConfigResolver}, loadChildren: () => import("@netway/home/home.module").then(value => value.HomeModule), }, { path: "pwa", canMatch: [platformGuard], canActivate: [authenticationGuard], resolve: {userConfig: userConfigResolver}, loadChildren: () => import("@netway/pwa/pwa.module").then(value => value.PwaModule), }, ]; </code>
const appRoutes: Routes = [
  {
    path: "home",
    canMatch: [platformGuard],
    canActivate: [authenticationGuard],
    resolve: {userConfig: userConfigResolver},
    loadChildren: () => import("@netway/home/home.module").then(value => value.HomeModule),
  },
  {
    path: "pwa",
    canMatch: [platformGuard],
    canActivate: [authenticationGuard],
    resolve: {userConfig: userConfigResolver},
    loadChildren: () => import("@netway/pwa/pwa.module").then(value => value.PwaModule),
  },
];
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export const PwaRoutes: Routes = [
{
path: "",
pathMatch: "full",
redirectTo: "tab/home"
},
{
path: "tab",
canActivate: [authenticationGuard],
loadComponent: () => import("@netway/pwa/pwa.component").then((x) => x.PwaComponent),
children: [
{
path: "home",
loadComponent: () => import("@netway/pwa/tab/home/pwa-home.component").then((x) => x.PwaHomeComponent),
canActivateChild: [authenticationChildGuard],
children: [
{
path: "",
pathMatch: "full",
title: "pwa.tab.home",
loadComponent: () => import("@netway/pwa/tab/home/home-main/pwa-home-main.component").then((x) => x.PwaHomeMainComponent)
},
{
path: "page",
loadComponent: () => import("@netway/pwa/tab/home/home-children/pwa-home-children.component").then((x) => x.PwaHomeChildrenComponent),
children: [
{
path: "userCustomHome",
title: "menu.dashboard",
component: UserCustomHomeComponent,
data: {layoutName: LayoutName.HOME}
},
{
path: "process",
loadChildren: () => import("../process/process.module").then(p => p.ProcessModule)
},
{
path: "widgetProcess",
loadChildren: () => import("../widget-process/widget-process.module").then(p => p.WidgetProcessModule)
},
{
path: "depositList",
title: "depositList.title",
loadChildren: () => import("../deposit-list/deposit-list.module").then(d => d.DepositListModule)
},
{
path: "billStatements",
title: "depositList.actionBar.billAccount",
component: DepositBillStatementComponent
},
{
path: "loan",
title: "loan.menu.title",
loadChildren: () => import("../loan/loan.module").then(l => l.LoanModule)
},
{
path: "cardlessCash",
title: "cardless.page.title",
loadChildren: () => import("../cardless-cash/cardless-cash.module").then(c => c.CardlessCashModule)
},
]
}
]
},
]
}
];
</code>
<code>export const PwaRoutes: Routes = [ { path: "", pathMatch: "full", redirectTo: "tab/home" }, { path: "tab", canActivate: [authenticationGuard], loadComponent: () => import("@netway/pwa/pwa.component").then((x) => x.PwaComponent), children: [ { path: "home", loadComponent: () => import("@netway/pwa/tab/home/pwa-home.component").then((x) => x.PwaHomeComponent), canActivateChild: [authenticationChildGuard], children: [ { path: "", pathMatch: "full", title: "pwa.tab.home", loadComponent: () => import("@netway/pwa/tab/home/home-main/pwa-home-main.component").then((x) => x.PwaHomeMainComponent) }, { path: "page", loadComponent: () => import("@netway/pwa/tab/home/home-children/pwa-home-children.component").then((x) => x.PwaHomeChildrenComponent), children: [ { path: "userCustomHome", title: "menu.dashboard", component: UserCustomHomeComponent, data: {layoutName: LayoutName.HOME} }, { path: "process", loadChildren: () => import("../process/process.module").then(p => p.ProcessModule) }, { path: "widgetProcess", loadChildren: () => import("../widget-process/widget-process.module").then(p => p.WidgetProcessModule) }, { path: "depositList", title: "depositList.title", loadChildren: () => import("../deposit-list/deposit-list.module").then(d => d.DepositListModule) }, { path: "billStatements", title: "depositList.actionBar.billAccount", component: DepositBillStatementComponent }, { path: "loan", title: "loan.menu.title", loadChildren: () => import("../loan/loan.module").then(l => l.LoanModule) }, { path: "cardlessCash", title: "cardless.page.title", loadChildren: () => import("../cardless-cash/cardless-cash.module").then(c => c.CardlessCashModule) }, ] } ] }, ] } ]; </code>
export const PwaRoutes: Routes = [
  {
    path: "",
    pathMatch: "full",
    redirectTo: "tab/home"
  },
  {
    path: "tab",
    canActivate: [authenticationGuard],
    loadComponent: () => import("@netway/pwa/pwa.component").then((x) => x.PwaComponent),
    children: [
      {
        path: "home",
        loadComponent: () => import("@netway/pwa/tab/home/pwa-home.component").then((x) => x.PwaHomeComponent),
        canActivateChild: [authenticationChildGuard],
        children: [
          {
            path: "",
            pathMatch: "full",
            title: "pwa.tab.home",
            loadComponent: () => import("@netway/pwa/tab/home/home-main/pwa-home-main.component").then((x) => x.PwaHomeMainComponent)
          },
          {
            path: "page",
            loadComponent: () => import("@netway/pwa/tab/home/home-children/pwa-home-children.component").then((x) => x.PwaHomeChildrenComponent),
            children: [
              {
                path: "userCustomHome",
                title: "menu.dashboard",
                component: UserCustomHomeComponent,
                data: {layoutName: LayoutName.HOME}
              },
              {
                path: "process",
                loadChildren: () => import("../process/process.module").then(p => p.ProcessModule)
              },
              {
                path: "widgetProcess",
                loadChildren: () => import("../widget-process/widget-process.module").then(p => p.WidgetProcessModule)
              },
              {
                path: "depositList",
                title: "depositList.title",
                loadChildren: () => import("../deposit-list/deposit-list.module").then(d => d.DepositListModule)
              },
              {
                path: "billStatements",
                title: "depositList.actionBar.billAccount",
                component: DepositBillStatementComponent
              },
              {
                path: "loan",
                title: "loan.menu.title",
                loadChildren: () => import("../loan/loan.module").then(l => l.LoanModule)
              },

              {
                path: "cardlessCash",
                title: "cardless.page.title",
                loadChildren: () => import("../cardless-cash/cardless-cash.module").then(c => c.CardlessCashModule)
              },
            ]
          }
        ]
      },
    ]
  }
];

The children hirearchy in "home/page/" are exactly the same as the ones in "pwa/tab/home/page/".
In my code I navigated to the pathes somehow like this: this.router.navigate("home/page/[path to the desired child]). As you see there’s no sign of PWA path because it has been added to the source code recently.

The role of platformGuard is to redirect all the pathes from home to pwa when the device is a supported gadget.

Now, my question is how to route to PWA routes without modifing dozens of this.router.navigate() absolute routing calls in my source code?

Currently, I introduced a new service and use it whenever I have to route in an absolute way:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Injectable({
providedIn: 'root'
})
export class PathService {
private readonly platformService = inject(PlatformService);
/**
* @desc Use this method when you have to navigate from one module to another one.
*
* IMPORTANT: Don't use this method when you can solve your issue using relative routes.
* @param partialPath The raw absolute path which needs to be modified based on the current mode (NW or PWA).
* @return The modified absolute path based on the current mode as an array of strings
*/
getAbsolute(...partialPath: ReadonlyArray<string>): Array<any> {
partialPath = this.format(...partialPath);
const pathPrefix = this.platformService.isPwaMode() ? ["pwa", "tab"] : [];
return [...pathPrefix, ...partialPath];
}
/**
* @desc Removes all the route slashes and flattens the path segments to a single array
*/
private format(...partialPath: Array<string>) {
return partialPath.flatMap(
pathSegment => pathSegment.split("/").filter(segment => segment !== "")
);
}
}
</code>
<code>@Injectable({ providedIn: 'root' }) export class PathService { private readonly platformService = inject(PlatformService); /** * @desc Use this method when you have to navigate from one module to another one. * * IMPORTANT: Don't use this method when you can solve your issue using relative routes. * @param partialPath The raw absolute path which needs to be modified based on the current mode (NW or PWA). * @return The modified absolute path based on the current mode as an array of strings */ getAbsolute(...partialPath: ReadonlyArray<string>): Array<any> { partialPath = this.format(...partialPath); const pathPrefix = this.platformService.isPwaMode() ? ["pwa", "tab"] : []; return [...pathPrefix, ...partialPath]; } /** * @desc Removes all the route slashes and flattens the path segments to a single array */ private format(...partialPath: Array<string>) { return partialPath.flatMap( pathSegment => pathSegment.split("/").filter(segment => segment !== "") ); } } </code>
@Injectable({
  providedIn: 'root'
})
export class PathService {
  private readonly platformService = inject(PlatformService);

  /**
   * @desc Use this method when you have to navigate from one module to another one.
   *
   * IMPORTANT: Don't use this method when you can solve your issue using relative routes.
   * @param partialPath The raw absolute path which needs to be modified based on the current mode (NW or PWA).
   * @return The modified absolute path based on the current mode as an array of strings
   */
  getAbsolute(...partialPath: ReadonlyArray<string>): Array<any> {
    partialPath = this.format(...partialPath);
    const pathPrefix = this.platformService.isPwaMode() ? ["pwa", "tab"] : [];
    return [...pathPrefix, ...partialPath];
  }

  /**
   *  @desc Removes all the route slashes and flattens the path segments to a single array
   */
  private format(...partialPath: Array<string>) {
    return partialPath.flatMap(
      pathSegment => pathSegment.split("/").filter(segment => segment !== "")
    );
  }
}

One sample usage:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> this.router.navigate(
this.pathService.getAbsolute("home", "page", "kartablRegister"),
{
queryParams: { kartablId: kartablId, asEditing: true }
}
);
</code>
<code> this.router.navigate( this.pathService.getAbsolute("home", "page", "kartablRegister"), { queryParams: { kartablId: kartablId, asEditing: true } } ); </code>
      this.router.navigate(
        this.pathService.getAbsolute("home", "page", "kartablRegister"),
        {
          queryParams: { kartablId: kartablId, asEditing: true }
        }
      );

The drawback of this solution is that I need to modify all thos this.router.navigate() calls.

How can I achieve the same result without the need to modify my existing this.router.navigate() calls?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật