连接字符串中的变量以形成URL

时间:2017-12-02 14:28:44

标签: angular typescript

我尝试根据变量使用必须特定于给定用户的API:他们的电磁炉ID。

因此,下面是我为了达到这个目的而编写的脚本:我在字符串中将变量解析为${cookieuid}以形成URL;

getBellNotification() {
    let cookieuid = this.currentUser;
    let headers = new Headers();
    this.authorizeHeaders(headers);
    return this.http.get(
        this.config.apiUrl + 'api/supplier-notification/${cookieuid}?_format=json', {
            headers: headers
        }
    )
        .map(response => response.json())
        .catch(this.handleErrorObservable);
}

由于我得到了下面的结果,并且有不受欢迎的特殊字符,因此无法正常工作:

http://<url>/api/supplier-notification/$%7Bcookieuid%7D?_format=json

我做错了什么,我怎么能做到?

我已经尝试过其他方法,但仍然无法让它发挥作用。

1 个答案:

答案 0 :(得分:1)

试试这个

getBellNotification(){
        let cookieuid = this.currentUser;
        const url = `${this.config.apiUrl}/api/supplier-notification/${cookieuid}?_format=json`;
        let headers = new Headers();
        this.authorizeHeaders(headers);
        return this.http.get(url, { headers: headers })
        .map(response => response.json())
        .catch(this.handleErrorObservable);
    }