代码之家  ›  专栏  ›  技术社区  ›  Bergrebell

Angular2 queryParams抛出“does not exist on type”错误

  •  4
  • Bergrebell  · 技术社区  · 10 年前

    我得到了一个Angular2服务,该服务应该从如下URL返回参数 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;
      }
    }
    

    但当我调用服务的getParameter()方法时,我得到了错误:

    error TS2339: Property 'foobar' does not exist on type '{ [key: string]: any; }'
    

    出错后返回正确的结果。我可以使用调试器访问foobar参数,而不会出现任何问题。有人有类似的问题吗?我可以想象它被提前调用了?

    (可能重复: angular2 rc6: Property queryParams does not exist on type RouterState )

    1 回复  |  直到 10 年前
        1
  •  9
  •   Stefan Svrkota    9 年前

    这是获取参数的方法:

    this.route.snapshot.queryParams['foobar'];
    
    推荐文章