输入'{httpHeaders:typeof HttpHeaders; }'与类型'{headers没有共同的属性:HttpHeaders |

时间:2018-12-20 14:36:20

标签: angular

您好,我尝试使用CRUD操作,但是它给出了'option'中的错误,我该如何解决。我写了下面的代码之后,它给出了以下错误。我使用了HttpClientHttpHeaders,但后来也没用。

  

Type'{httpHeaders:typeof HttpHeaders; }'在中没有属性   类型'{headers?常见:HttpHeaders | {[header:string]:字符串   |串[]; };观察?参数?:HttpParams | {[参数:   字符串]:字符串|串[]; }; reportProgress ?:布尔值;   responseType ?:“ json”; withCredentials ?:布尔值; }'。

我已经编写了以下代码,用于使用API​​执行CRUD操作。

        import { Injectable } from '@angular/core';
            import { HttpClient } from '@angular/common/http';
            import { HttpHeaders } from '@angular/common/http';

            import { Observable } from 'rxjs';
            import { Employee } from './employee';


            @Injectable({
              providedIn: 'root'
            })
            export class EmployeeService {
              url = 'http://localhost:65389/api/EmaployeeMasters';
              constructor(private http: HttpClient) { }
              getAllEmployee(): Observable<Employee[]> {
                return this.http.get<Employee[]>(this.url);
              }

              getEmployeeById(employeeid: string): Observable<Employee> {
                return this.http.get<Employee>(this.url + '/' + employeeid);
              }
              createEmployee(employee: Employee): Observable<Employee> {
                const httpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
                const options = {
                  httpHeaders: HttpHeaders
                };
                return this.http.post<Employee>(this.url, employee, options);
              }

              updateEmployee(employee: Employee): Observable<number> {
                const httpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
                const options = {
                  httpHeaders: HttpHeaders
                };
                return this.http.post<number>(this.url + '/' + employee.ID, employee, options);
              }
              DeleteEmployeeById(employeeid: string): Observable<number> {
                const httpHeaders = new HttpHeaders()
                  .set('Content-Type', 'application/json');
                return this.http.delete<number>(this`enter code here`.url + '/' + employeeid);
              }
            }

2 个答案:

答案 0 :(得分:2)

我认为您的HttpHeaders有一些错误。 试试这样的东西:

createEmployee(employee: Employee): Observable<Employee> {
  const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
  return this.http.post<Employee>(this.url, employee, httpOptions);
}

updateEmployee(employee: Employee): Observable<number> {
  const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
  return this.http.post<number>(this.url + '/' + employee.ID, employee, httpOptions);
}

您的DeleteEmployeeById函数上还有一些错字。也许就像:

deleteEmployeeById(employeeid: string): Observable<number> {
   const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
   return this.http.delete<number>(this.url + '/' + employee.ID, httpOptions);
}

答案 1 :(得分:0)

我认为是这样的:

 const options = {
              httpHeaders: HttpHeaders
            };

应该是

 const options = {
              headers: HttpHeaders
            };