File

libs/core/src/lib/services/taly-routing-utils.service.ts

Index

Methods

Constructor

constructor(router?: Router)
Parameters :
Name Type Optional
router Router Yes

Methods

getPossibleJourneyPaths
getPossibleJourneyPaths()
Returns : string[]
import { Inject, Injectable, Optional } from '@angular/core';
import { Route, Router } from '@angular/router';

@Injectable()
export class TalyRoutingUtilsService {
  constructor(@Optional() @Inject(Router) private router?: Router) {}

  getPossibleJourneyPaths(): string[] {
    if (!this.router) {
      throw new Error(
        '"Router" is not available. Please provide it when creating a new instance of "TalyRoutingUtilsService".'
      );
    }
    const routerConfig = this.router.config;

    const possibleJourneyPaths: string[] = [];
    for (const route of routerConfig) {
      this.traverseAngularRoute(route, '', possibleJourneyPaths);
    }

    return possibleJourneyPaths;
  }

  protected traverseAngularRoute(route: Route, currentPath: string, paths: string[]) {
    if (route.path) {
      currentPath += '/' + route.path;
      paths.push(currentPath);
    }

    if (route.children && route.children.length > 0) {
      // Recursively build paths for each child in the "children" array
      for (const child of route.children) {
        this.traverseAngularRoute(child, currentPath, paths);
      }
    }
  }
}

results matching ""

    No results matching ""