简体   繁体   中英

Angular 4 http get with query parameters

The first block is working as expected

getQuotes(): Observable<Quote[]> {
    return this.http.get(this.url)
        .map((res: Response) => res.json())
        .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}

Now I am tring to add query params to this.url and the url have not changed

getQuotes2(): Observable<Quote[]> {
    let myParams  = new URLSearchParams();
    myParams.append('author', 'authorName');
    myParams.append('catid', '123');
    let options = new RequestOptions({ params: myParams });

    return this.http.get(this.url, options )
        .map((res: Response) => res.json())
        .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}

I have checked in devtools. Caching is switched off. I have tried { search: myParams }, with RequestOptions and return this.http.get(this.url, { params: myParams } ) Where I am not looking i see string concatination. These parameters are optinal and I append them on condition.

你有进口params吗?

import { URLSearchParams } from '@angular/http';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM