Peter Piper Peter Piper - 2 months ago 36
Javascript Question

Angular2 queryParams throws "does not exist on type" error

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)

Answer

This is how you get parameters:

this.route.snapshot.queryParams['foobar'];

Right now, you are trying to access it like it is an attribute of queryParams.

Comments