我需要知道下面我的代碼中這個錯誤的含義是什么。將資料傳遞給 db 是 Post 方法,并且發生此錯誤是因為“型別 'User' 上不存在屬性 'json'”。檢查下面的代碼。
Angular 版本:12.2.12 節點:16.13.0 包管理器:npm 8.1.2
// model.ts
export class User{
UserID : number
UserName : string
FirstName : string
LastName : string
Email : string
Password : string
ConfrimPass : string
UserRole : string
Authorize_building :string
}
//component.ts
resetForm(form? : NgForm) {
if (form != null)
form.reset();
this.userService.selectUser = {
UserID: null,
UserName: '',
FirstName: '',
LastName: '',
Email: '',
Password: '',
ConfrimPass: '',
UserRole: '',
Authorize_building: '',
}
}
//service.ts
PostUser(usr : User): Observable<User>{
var body = JSON.stringify(usr);
var headerOption = new Headers({ 'Content-Type' : 'application/json' });
var requestOption = new RequestOPtions({ method: RequestMethod.Post, headers: headerOption });
// return this.http.post('https://localhost:44339/api/Users', body, requestOption).map(x => x.json());
var headersOption = ({ 'Content-Type' : 'application/json' });
var body = JSON.stringify(usr);
return this.http.post<User>('https://localhost:44339/api/Users', body, { headers: headersOption } ).map(x => x.json());
};
}
我需要一個解決方案!
uj5u.com熱心網友回復:
我會嘗試通過型別斷言回應:
從...
.map(x => x.json());
我會做
.map((x: Type) => x.json())
uj5u.com熱心網友回復:
嘗試在管道運算子中使用 map ,如下所示。
.pipe(map((response) => response.json()))
也像這樣從 RxJ匯入map和匯入pipe。
import { map, pipe } from 'rxjs/operators'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/352696.html
上一篇:Angular-錯誤的包版本?
