0

I have the following routing configuration in my Angular 6 project.

    path: 'chart/:feature',
    component: ChartStateComponent,
    canActivateChild: [ChartGuardService],
    children: [
        {
            path: ':id',
            redirectTo: '/chart/:feature/:id/profile',
            pathMatch: 'full'
        },
        {   path: ':id/:tab',
            component: TestOneComponent,
            pathMatch: 'full'
        }
    ]

feature is param to parent path and it can 4 different values like one, two, three and four. Now depending on this param, I need to set the component for children path.

For example, if feature is set one, then set TestOneComponent as children component.

How can i achieve this?

1 Answer 1

0

You could try this:

path: 'chart',
component: ChartStateComponent,
canActivateChild: [ChartGuardService],
children: [
    {
        path: 'one/:id',
        redirectTo: 'one/:id/profile',
        pathMatch: 'full'
    },
    {   path: 'one/:id/:tab',
        component: TestOneComponent
    },
    {   path: 'two/:id/:tab',
        component: TestTwoComponent
    },
    ...
],

Alternatively, you could build a route guard to handle more complex routing. See this question for more information: Angular2 conditional routing

UPDATE

I'm not sure how you could add a conditional based on the parameter ... but you can add a conditional based on a global variable.

In a "Globals" file:

 export class Globals {
    public static ISADMIN: boolean = false;
 }

In your module route definitions

  {
    path: 'products/:id',
    component: Globals.ISADMIN ?  ProductAdminComponent : ProductDetailComponent
  },
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply. Though I have solved the issue using Guards. But still curious know is there way to set the components based on parameter?
Not that I know of. But you can use a global variable. See my updated answer.
Yeah, global variables did worked for me. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.