1

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.

shinvu
  • 601
  • 2
  • 7
  • 23
  • Yup, the answer to the duplicate redirect solved my problem. Although, because of the re-factoring they've done with `operators` in RxJS recently, the syntax has changed a bit. – shinvu Nov 14 '18 at 13:44
  • 2
    The import statement becomes `import {first} from "rxjs/operators"` and the `toPromise()` bit becomes `this.currentActivateRoute.queryParams.pipe(first()).toPromise().then()` – shinvu Nov 14 '18 at 13:48

0 Answers0