2016-09-20 114 views
3

我得到了一个Angular2服务应该从URL像http://localhost:3001/?foobar=1236Angular2 queryParams抛出错误

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

回答

5

这是你如何得到参数:

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

感谢的人!发布后两分钟我发现它...始终如此:/ –