【问题标题】:Angular 2 error:Supplied parameters do not match any signature of call targetAngular 2 错误:提供的参数与调用目标的任何签名都不匹配
【发布时间】:2017-06-08 23:35:11
【问题描述】:

我正在我的 Angular 2 应用程序中构建一个 RESTful 服务以连接到远程后端。

我的 GET (all) /POST 方法似乎没问题,我的 PUT 方法似乎没问题,但是我的 GET(单个对象)方法,看起来就像 PUT 方法,抛出上述错误:提供的参数做与调用目标的任何签名都不匹配。

这会在编译过程中引发错误:

    getCourse (course: Course): Observable<Course> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    let courseUrl = this.BaseUrl + '/' + course.id;
    return this.http.get(courseUrl, { course }, options)
                .map(this.extractData)
                .catch(this.handleError);
    }

这行得通:

put(course: Course): Observable<Course> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    let courseUrl = this.BaseUrl + '/' + course.id;
    return this.http.put(courseUrl, { course }, options)
                .map(this.extractData)
                .catch(this.handleError);
    }

想法?我做错了什么?

【问题讨论】:

    标签: angular typescript rxjs angular2-observables


    【解决方案1】:

    get 不支持body 参数

    return this.http.get(courseUrl, { course }, options)
    

    应该是

    return this.http.get(courseUrl, options)
    

    https://angular.io/docs/ts/latest/api/http/index/Http-class.html#!#get-anchor

    get(url: string, options?: RequestOptionsArgs) : Observable

    https://angular.io/docs/ts/latest/api/http/index/Http-class.html#!#put-anchor

    put(url: string, body: any, options?: RequestOptionsArgs) : Observable

    【讨论】:

    • 发布问题后,我立即意识到这是问题所在。谢谢!
    猜你喜欢
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多