首页 > 解决方案 > Angular HttpClient 不发送 gettter 提供的属性

问题描述

学生.ts

...

private _status: string;

...

get status() {
  return this._status;
}

...


学生服务.ts

...

save(student: Student): Observable<Student> {
  const postStudentURL = 'http://localhost:8080/api/students';
  return this.httpClient.post<Student>(postStudentURL, student);
}

...


JSON 发送到后端

{
...

"_status": "Valid",

...
}


为什么不发送状态,而是发送私有 _status ?

标签: angulartypescript

解决方案


return this.httpClient.post<Student>(postStudentURL, student);

上面的行发送json学生对象的表示。

所以请求包含字段而不是getter。

为了确认,您可以手动创建一个实例,Student然后console.log(JSON.stringify(student))


推荐阅读