1

I'm trying to differentiate between the sample companies and other companies. So I'm passing query params to check if it is sample company and check the query param sample if it is true in Route array like this

const routes: Routes = [{ path: '',
    component: DefaultComponent,
    children: [
        {path: 'company', redirectTo: 'company-data/1?sample=true'},
        {path:'company/:id', component: CompanyComponent}
]}];

How can I read this query param in component and fetch true value for sample company and for other companies that sample will be false. I get undefined if I try to fetch it like this

this.route.queryParams.subscribe(params => {
    console.log('Query params ',params['sample']) 
});

I don't know if this is right way.

3
  • there's no component for 'company-data/' route? Commented Oct 25, 2017 at 10:05
  • @NadunLiyanage It is redirected to company/:id, hence have a component 'CompanyComponent' Commented Oct 25, 2017 at 10:13
  • are you always redirecting with id=1? Commented Oct 25, 2017 at 10:21

2 Answers 2

4

queryParams are optional parameters. You do not need to specify them in the Routes module. But for this, you need to be sure that you not only pass the id=1

const routes: Routes = [{ path: '',
component: DefaultComponent,
children: [

    {path:'company/:id', component: CompanyComponent}
]}];

and you can simply call the component by router.navigate(['/', 'company', 1], { queryParams: { sample: true } });

Sign up to request clarification or add additional context in comments.

Comments

0

import activatedRoute from your router module and assign query parameter inside your component

import { ActivatedRoute } from '@angular/router';

export class CompanyComponent { 

  isSample: boolean; 

  constructor(private routeParams: ActivatedRoute) { 
    this.isSample = routeParams.snapshot.params['sample']; 
  }

}

1 Comment

I used this, but it didn't solved my problem. Though I got a way out by nesting my routes and it solved this issue.

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.