Chriss
Chriss

Reputation: 5639

Can't get query parameters from ActivatedRoute

I have a route defined like this in the routing module:

{ path: 'warning/:type', component: WarningPageComponent }

When the app wants to navigate to the warning page this call is made:

const navigationExtras: NavigationExtras = {
        queryParamsHandling: 'preserve',
        queryParams: { errorCode: '12345'}
      };
return this.router.navigate(['/warning/default'],navigationExtras);

In the WarningPageComponent the parameters are read like this ngOnInit():

const params = this.route.snapshot.paramMap;
console.log('ERR warning page' + JSON.stringify(params));
const errorCode = params.get('errorCode') ?? undefined;
const type = params.get('type') ?? undefined;

The problem is now that only the type is present in the paramMap but not the errorCode. What is wrong here? I also tried with queryParamsHandling: 'preserve'

Angular 8.2.14

I found no solution in:

Upvotes: 0

Views: 4783

Answers (2)

Sangar Lal
Sangar Lal

Reputation: 50

Do like this get the params inside constructor

import { ActivatedRoute } from '@angular/router';
    export class DashboardComponent implements OnInit, AfterViewChecked, OnDestroy {
    queryParams: any;
    constructor(private activatedRoute: ActivatedRoute) {
        this.queryParams = Utils.paramsToLower(this.activatedRoute.snapshot.queryParams);
      }
    }

Upvotes: 0

Chellappan வ
Chellappan வ

Reputation: 27471

If you want to access queryParams then you have to use queryParamMap on snapshot object.

const queryParams = this.route.snapshot.queryParamMap;
const errorCode = queryParams.get('errorCode') ?? undefined;

Upvotes: 1

Related Questions