8

I have this route with the query param lang:

http://localhost:4200/?lang=de

None of the following examples work, it is always null or undefined

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

constructor(private route: ActivatedRoute) { 
  console.log(this.route.snapshot.params.lang);
  console.log(this.route.snapshot.queryParamMap.get('lang'));
}

ngOnInit(): void {
  this.route.queryParams.subscribe(params => {
    let lang = params['lang'];  
    console.log(lang);
  });
  console.log(this.route.snapshot.queryParamMap.get('lang'));
  console.log(this.route.snapshot.params.lang);
}

What am I doing wrong? I have now read several guides, even some for angular 10. Each of them show the above examples, but I still cannot get the value of lang.

3
  • What is your version of angular ? Commented Aug 18, 2020 at 12:26
  • Did you try checking the Value of de , before passing it as a param Commented Aug 18, 2020 at 12:34
  • can you try this this.route.snapshot.paramMap.get('lang'); Commented Aug 18, 2020 at 12:53

4 Answers 4

8

Here is my working solution:

var url = new URL(window.location.href);
var lang = url.searchParams.get("lang");

This works in ngInit and in constructor.

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

2 Comments

This also works in app.component without router-outlet!
This solution isn't correct for SSR.
3

For the Observable mechanism, you can use paramMap observable like this with an undefined check.

this.route.paramMap.subscribe(params => {
if(params.get('lang')){
    let lang = params.get('lang');  
    console.log(lang);
    }
  });

And other solutions @Leo has already proposed.

Thanks.

1 Comment

Do you know if this method can be used without using the current route? For example: this.router.parseUrl('https://myapiendpoint.com?page=2');
1

What about

this.route.snapshot.paramMap.get('lang');

From the official documentation ?

3 Comments

Did you try it in constructor or ngOnInit ?
I have tried in in constructor, but I found an other solution, see my answer
Ok, glad you found your answer. I would still try my answer in ngOnInit though, as it more the "angular way".
0
  constructor(private route: ActivatedRoute,
              private router: Router) {
    this.route.queryParamMap.pipe(
      filter(queryParamMap => queryParamMap.has('lang'))
    ).subscribe(queryParamMap => {
      const lang = queryParamMap.get('lang');
      console.log(lang);
    });

    this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      switchMapTo(this.route.queryParamMap)
    ).subscribe(queryParamMap => {
      const lang = queryParamMap.get('lang');
      console.log(lang);
    });
  }

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.