Home > Blockchain >  How to Set ? in routeFile in Angular
How to Set ? in routeFile in Angular

Time:08-03

My Requirement is From Backend I am getting routes as "app/dashboard?dashboard_id={id}" How can I configure this in Module.ts file?

I tried using below

const routes: Routes = [
  {
    path: "app/dashboard/:dashboard_id",
    component: AddEditComponent,
    canActivate: [AuthGuard],
  },
];

but I am getting errors like routes are not defined.

Can Someone Please Help me on that how can I configure this route as I need to catch this id as queryParams in Component.

CodePudding user response:

You can do something like this:

const routes: Routes = [
  {
    path: "app/dashboard",
    component: AddEditComponent,
    canActivate: [AuthGuard],
    children: [
        {
            path: ':dashboard_id'
            component: NewComponentId
        }
    ]
  },
];

and in your NewComponentId you can do something like inside the constructor to catch the id:

this.route.paramMap.pipe(
      map((paramMap) => {
        if (paramMap.get('id') !== 'something') {
          // your code
        }
      }),

CodePudding user response:

Required Route param:

{path: 'users/:userId', component: UserComponent}

and get it from param:

constructor(params: RouteParams) {
    var paramId = params.get("id");
}

Optional Route Param:

  { path: '/user', component: UserComponent }

its just define the route part and param pass by query string, to read the queryparam:

this.route.queryParams
      .subscribe(params => {
        console.log(params);
      }
    );

you must process the query string for this route: "app/dashboard?dashboard_id={id}"

  • Related