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';
6
  • 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. Commented Mar 1, 2017 at 15:47
  • You can see how to access query parameters from your code here: stackoverflow.com/questions/35688084/… Commented Mar 1, 2017 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. Commented Mar 1, 2017 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. Commented Mar 1, 2017 at 15:53
  • Okay, thanks for that information...I'm pretty new to angular2/typescript so probably making a lot of mistakes. Commented Mar 1, 2017 at 15:55

1 Answer 1

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?

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

Comments

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.