最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 queryParams throws "does not exist on type" error - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 9

This is how you get parameters:

this.route.snapshot.queryParams['foobar'];
发布评论

评论列表(0)

  1. 暂无评论