【问题标题】:How to concat url in Angular 6?如何在 Angular 6 中连接 url?
【发布时间】:2018-09-25 08:48:58
【问题描述】:

我有 common-class 有 commonUrl,我在 category.service.ts 中使用了这个 commonUrl,但在 service.ts 中没有 concat 如何在 angular 6 中连接这个 commonUrl?

common-class.ts

export class CommonClass {
  constructor(public commonUrl : string = 'http://localhost:3000'){};
}

category.service.ts

import { CommonClass } from '../classes/common-class';
commonUrlObj : CommonClass = new CommonClass();

saveNewCategory(formData){
  return this.http.post('this.commonUrlObj.commonUrl'+''+'/saveNewCategory',formData).map((res: any) => res.json());
}

getCategoryDetails(param){
  return this.http.post('this.commonUrlObj.commonUrl'+''+'getCategoryDetails',param).map((res: any) => res.json());
}

【问题讨论】:

  • 我觉得应该是return this.http.post(this.commonUrlObj.commonUrl +'/saveNewCategory' + formData).map((res: any) => res.json());

标签: javascript string typescript


【解决方案1】:

我建议您使用字符串文字。使用`,导致`${this.commonUrlObj.commonUrl}/saveNewCategory`

【讨论】:

  • 推荐使用上述模板文字。如果你使用+,如果你运行Sonar之类的工具会导致代码异味缺陷。
【解决方案2】:

'this.commonUrlObj.commonUrl' 中删除single quotes

saveNewCategory(formData){
  return this.http.post(this.commonUrlObj.commonUrl+'/saveNewCategory',formData).map((res: any) => res.json());
}

【讨论】:

    【解决方案3】:

    请删除单引号,它应该可以工作。

    return this.http.post(this.commonUrlObj.commonUrl+'/saveNewCategory',formData).map((res: any) => res.json());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多