Rich
Rich

Reputation: 6561

Access querystring param in angular2 with this.activeRoute.queryParams._value

When debugging in chrome I can see this object:

this.activeRoute.queryParams._value

activeRoute is passed in my constructor as private activeRoute: ActivatedRoute

When I'm in vs code and type this.activeRoute.queryParams, ._value isn't an available object. Is there a away to access that object?


Just wanted to post the code I ended up using.

this.activeRoute.queryParams.subscribe((params: Params) => {
    let context = params['context'];
    this.context = context;
  });

also don't forget to add Params to the import statement

 import {Router, ActivatedRoute, Params} from '@angular/router';

Upvotes: 1

Views: 5986

Answers (1)

Kallum Tanton
Kallum Tanton

Reputation: 777

Here's an answer based on my comments.

_value is a private member of queryParams (not sure what the type is) so the private scope means it cannot be accessed outside of the defining class.

You may well be aware that JavaScript has no notion of public and private scope so this means that it is technically possible to access this member using your code however the TypeScript compiler will refuse to transpile your code into JavaScript as that DOES use private scope.

The answer to this question gives a suitable way to get query string parameters: How get query params from url in angular2?

Upvotes: 2

Related Questions