1

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';
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
Rich
  • 6,470
  • 15
  • 32
  • 53
  • 1
    `_value` is a private member of `queryParams` - only the class itself can access it. Since JavaScript has no notion of private or public scope you could technically access the member in your code however TypeScript will not allow you to compile it, so it won't work. – Kallum Tanton Mar 01 '17 at 15:47
  • You can see how to access query parameters from your code here: http://stackoverflow.com/questions/35688084/how-get-query-params-from-url-in-angular2 – Kallum Tanton Mar 01 '17 at 15:49
  • Okay thanks for the information. I am trying to get a querystring parameter currently with this.activeRoute.snapshot.queryParams['context'] but it's not getting the correct value. I think it's getting a previous value. I did notice though in chrome that this.activeRoute.queryParams._value.context does contain the correct value. – Rich Mar 01 '17 at 15:50
  • 1
    Make sure you subscribe to changes as opposed to just getting the value, otherwise you'll always get an outdated value. – Kallum Tanton Mar 01 '17 at 15:53
  • Okay, thanks for that information...I'm pretty new to angular2/typescript so probably making a lot of mistakes. – Rich Mar 01 '17 at 15:55
  • I understand, it takes a lot of time to understand how Angular works in it's entirety. Stick with it though, there's not much that you can't do with it. – Kallum Tanton Mar 01 '17 at 16:01

1 Answers1

2

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?

Community
  • 1
  • 1
Kallum Tanton
  • 802
  • 7
  • 22