Can't Read Query Parameters From Promise

562 views Asked by At

I am trying to read the query parameters from a URL in Angular like so:

export class QueryParameterReader implements OnInit {
  constructor(private currentActiveRoute: ActivatedRoute) {}

  ngOnInit() {
    this.currentActiveRoute.queryParams.subscribe((queryParams: Params) => {
      console.log("Recieved query parameters: " + queryParams['userid']);
    });
  }
}

But as soon as I try to do it through a Promise, it fails (the then handler never fires):

export class QueryParameterReader implements OnInit {
  constructor(private currentActiveRoute: ActivatedRoute) {}

  ngOnInit() {
    this.currentActiveRoute.queryParams.toPromise().then((queryParams: Params) => {
      console.log("Recieved query parameters: " + queryParams['userid']);
    });
  }
}

Any idea why this might be happening? It seems to me that I should be doing this the second way, because I want to read the query parameters only once; it doesn't make sense to keep an Observable open the whole time.

0

There are 0 answers