我尝试用角度拨打我的后端电话。 我已经有方法获取/删除作品。
但是我对“ POST”有疑问。
我使用swaggercode-gen在TS中生成所有类。
但是当我调用对我的create函数的调用时,我收到以下消息:“无法将Employee类型的消息参数分配给event类型的参数”
员工是我自己的班级。
我尝试了很多事情,尝试更改表单,尝试更改打字稿文件(指示不要这样做)。
Swagger-codegen为我生成了employee.ts文件,该文件可以。
它还会使用我的创建功能生成一个employee.service.ts文件:
employe.service.ts:
public createEmployee(observe?: 'body', reportProgress?: boolean): Observable<any>;
public createEmployee(observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<any>>;
public createEmployee(observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<any>>;
public createEmployee(observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
let headers = this.defaultHeaders;
// to determine the Accept header
let httpHeaderAccepts: string[] = [
];
const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAcceptSelected != undefined) {
headers = headers.set('Accept', httpHeaderAcceptSelected);
}
// to determine the Content-Type header
const consumes: string[] = [
'application/json'
];
return this.httpClient.post<any>(`${this.basePath}/employees/createEmployee`,
null,
{
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}
我的表单:
employee-form.component.html:
<form #employeeForm="ngForm" (ngSubmit)="create()">
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Matricule</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="idEmployee" [(ngModel)]="employee.idEmployee" name="idEmployee"/>
</div>
</div>
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Nom</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lastName" [(ngModel)]="employee.lastName" name="lastName"/>
</div>
</div>.
[...]
<div class="form-group row">
<div class="col-sm-1">
<button type="submit" class="btn btn-primary">Créer</button>
</div>
<div class="col-sm-1">
<a class="btn btn-secondary" href="javascript:history.go(-1)">Retour</a>
</div>
</div>
</form>
我的类型脚本文件:
employee-form.component.ts:
@Component({
selector: 'adv-employee-form',
templateUrl: './employee-form.component.html',
providers: [EmployeeService],
styles: []
})
export class EmployeeFormComponent implements OnInit {
employee: Employee;
constructor(private router: Router, private employeeService: EmployeeService) { }
ngOnInit() {
}
create() {
this.employeeService.createEmployee(this.employee)//<- The error appear here
.pipe(finalize(() => this.router.navigate(['/employees-list']))).subscribe();
}
}
似乎这个雇员不能正确识别。
我尝试更改employee-form.component.ts并设置:
employee: any = Employee;
错误消失。但是,当我发布表格时,出现错误:
ERROR Error: Unreachable: unhandled observe type [object Object]}
答案 0 :(得分:0)
根据您的代码,您的方法不希望使用类型为employee的参数,而且您正在传入正文,我认为您需要发布employee对象,因此在服务中创建employee方法应该是
public createEmployee(model:any,observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
let headers = this.defaultHeaders;
// to determine the Accept header
let httpHeaderAccepts: string[] = [
];
const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAcceptSelected != undefined) {
headers = headers.set('Accept', httpHeaderAcceptSelected);
}
// to determine the Content-Type header
const consumes: string[] = [
'application/json'
];
return this.httpClient.post<any>(`${this.basePath}/employees/createEmployee`,
model,
{
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}