如何在javascript中连接两个字符串?

时间:2019-05-22 12:44:27

标签: javascript angular

我想以角度7连接两个字符串。

函数是:

getEmployment(id: number): Observable<Employment> {
    const url = '${this.EmploymentUrl}/${id}';
    return this.http.get<Employment>(url).pipe(
      tap(_ => this.log('fetched employment id=${id}')),
      catchError(this.handleError<Employment>('getEmployment id=${id}'))
    );
  }

但是,当我在Web浏览器中检查元素时,它表明未找到{id}。

如果我将第二行替换为以下内容,则效果很好。

const url = this.EmploymentUrl + '/' + id;

经过大量的搜索后,我无法弄清楚第一种方法为什么不起作用。为什么不起作用?这两种方法有什么区别?

4 个答案:

答案 0 :(得分:0)

您应该使用``而不是引号。更多信息,请点击https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

NSDictionary

答案 1 :(得分:0)

因为您使用引号(')而不是反引号(`)

.posts {
  list-style-image: url("{{ with .Params.thumbnail }}{{ . }}{{ end }}");
}

答案 2 :(得分:0)

答案很简单,我必须使用背包(`)而不是单引号(')

像这样:

 const url = `${this.EmploymentUrl}/${id}`;

答案 3 :(得分:0)

或者您可以只做一个简单的字符串concat之类的

const url:string = this.Employment+'/'+id; // id will be converted into a string