Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular 6中连接url?_Javascript_String_Typescript - Fatal编程技术网

Javascript 如何在Angular 6中连接url?

Javascript 如何在Angular 6中连接url?,javascript,string,typescript,Javascript,String,Typescript,我有一个公共类有commonUrl,这个commonUrl是我在category.service.ts中使用的,但它不是在service.ts中使用的concat。如何在第6节中使用这个commonUrl 通用类 export class CommonClass { constructor(public commonUrl : string = 'http://localhost:3000'){}; } 类别.服务.ts import { CommonClass } from '../cl

我有一个公共类有commonUrl,这个commonUrl是我在category.service.ts中使用的,但它不是在service.ts中使用的concat。如何在第6节中使用这个commonUrl

通用类

export class CommonClass {
  constructor(public commonUrl : string = 'http://localhost:3000'){};
}
类别.服务.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());
}

'this.commonUrlObj.commonUrl'中删除
单引号

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

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

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

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

我认为应该
返回this.http.post(this.commonUrlObj.commonUrl+'/saveNewCategory'+formData).map((res:any)=>res.json())建议使用上述模板文本的可能重复。如果您使用
+
,如果您运行的是Sonar之类的工具,则会导致代码气味缺陷。