admin管理员组

文章数量:1287849

I have mon-class has monUrl, this monUrl i used in category.service.ts but it not concat in service.ts how to concat this monUrl in angular 6?

mon-class.ts

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

category.service.ts

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

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

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

I have mon-class has monUrl, this monUrl i used in category.service.ts but it not concat in service.ts how to concat this monUrl in angular 6?

mon-class.ts

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

category.service.ts

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

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

getCategoryDetails(param){
  return this.http.post('this.monUrlObj.monUrl'+''+'getCategoryDetails',param).map((res: any) => res.json());
}
Share Improve this question edited Sep 25, 2018 at 9:05 asked Sep 25, 2018 at 8:48 user10411207user10411207 2
  • 2 i think it should be return this.http.post(this.monUrlObj.monUrl +'/saveNewCategory' + formData).map((res: any) => res.json()); – Lia Commented Sep 25, 2018 at 8:52
  • 1 Possible duplicate of JS strings "+" vs concat method – Jota.Toledo Commented Sep 25, 2018 at 9:00
Add a ment  | 

3 Answers 3

Reset to default 7

I'd advice you to use a string literal. Using `, resulting in `${this.monUrlObj.monUrl}/saveNewCategory`

remove single quotes from 'this.monUrlObj.monUrl'

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

Please remove the single quotes and it should work.

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

本文标签: javascriptHow to concat url in Angular 6Stack Overflow