Ошибка при попытке выполнить операцию удаления в Angular

Когда я пытаюсь выполнить операцию удаления в операциях angular CRUD, я получаю следующую ошибку: ОШИБКА в src / app / home / home.component.ts (29,37): ошибка TS2322: тип «Заголовки» не назначается для type 'HttpHeaders | {[заголовок: строка]: строка | нить[]; } '. Тип 'Заголовки' нельзя присвоить типу '{[заголовок: строка]: строка | нить[]; } '. В типе "Заголовки" отсутствует подпись индекса. Вот код:

import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpResponse, HttpHeaders } from '@angular/common/http';  
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  products: any[];
  constructor(private http: HttpClient) {}
  id: number;
  private headers = new Headers({ 'Content-Type': 'application/json'});

  //Url to fetch data: http://localhost:3000/products
  getData(){
    this.http.get("http://localhost:3000/products")
      .subscribe(response=>{
        this.products = response as any[];
      });
  }

  deleteProduct(id){
    if(confirm("Are you sure?")){
      const url = `${"http://localhost:3000/products"}/${id}`;
      return this.http.delete(url, {headers: this.headers}).toPromise()
        .then(()=>{
          this.getData();
        })
    }
  }

  ngOnInit() {
    this.getData(); 
  }
}


person Soccerplayer97    schedule 05.01.2021    source источник
comment
Класс, используемый для заголовков, называется HttpHeaders (angular.io/api/common/http/HttpHeaders). Пример: https://angular.io/guide/http#adding-headers   -  person Fussel    schedule 05.01.2021


Ответы (1)


Как указано в сообщении об ошибке, ожидаемым типом является HttpHeaders, а не Headers:

private headers = new HttpHeaders({ 'Content-Type': 'application/json'});
person Gérôme Grignon    schedule 05.01.2021