I got an Angular2-service that should return parameters from an URL like http://localhost:3001/?foobar=1236
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/map';
@Injectable()
export class ParameterService {
constructor(private route: ActivatedRoute) { }
getParameter() {
return this.route.snapshot.queryParams.foobar;
}
}
But when I call the getParameter() method of the service I get the error:
error TS2339: Property 'foobar' does not exist on type '{ [key: string]: any; }'
and after the error it returns the correct result. I can access the foobar parameter with the debugger without any problems. did someone had a similar issue? I could imagine that it gets called to early?
(possible duplicate: angular2 rc6: Property queryParams does not exist on type RouterState)
I got an Angular2-service that should return parameters from an URL like http://localhost:3001/?foobar=1236
import { Injectable } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/map';
@Injectable()
export class ParameterService {
constructor(private route: ActivatedRoute) { }
getParameter() {
return this.route.snapshot.queryParams.foobar;
}
}
But when I call the getParameter() method of the service I get the error:
error TS2339: Property 'foobar' does not exist on type '{ [key: string]: any; }'
and after the error it returns the correct result. I can access the foobar parameter with the debugger without any problems. did someone had a similar issue? I could imagine that it gets called to early?
(possible duplicate: angular2 rc6: Property queryParams does not exist on type RouterState)
Share Improve this question edited May 23, 2017 at 10:31 CommunityBot 11 silver badge asked Sep 20, 2016 at 11:25 Dennis NedryDennis Nedry 4,7774 gold badges45 silver badges58 bronze badges 1-
At 2019 with angular 8 you could use
this.route.params.subscribe((params) => { const foobar = params.foobar; })
– ontananza Commented Nov 17, 2019 at 0:54
1 Answer
Reset to default 9This is how you get parameters:
this.route.snapshot.queryParams['foobar'];