如何在Angular 6中连接URL?

时间:2018-09-25 08:48:58

标签: javascript string typescript

我有common-class有commonUrl,我在category.service.ts中使用了这个commonUrl,但是在service.ts中却没有连接,如何在角度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());
}

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

single quotes删除'this.commonUrlObj.commonUrl'

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

答案 2 :(得分:0)

请删除单引号,它应该起作用。

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