当我尝试通过服务和接口从json中获取数据时出现此错误。在Visual Studio代码中,它未显示任何错误,而在编译时则显示错误'属性'数据'在类型上不存在“承诺”
import { ICar } from './ICar';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map} from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class CarsService {
constructor(private http: HttpClient) {
}
getCars(): Observable<ICar[]> {
return this.http.get('./app/cars.json')
.pipe(map((resp: Response) => resp.json().data));
}
}
import { ICar } from '../ICar';
import { CarsService } from '../cars.service';
import { Component, OnInit} from '@angular/core';
@Component({
// tslint:disable-next-line:component-selector
selector: 'cars',
templateUrl: 'app/cars/cars.component.html',
providers: [CarsService]
})
export class CarsComponent implements OnInit {
car: ICar[] = null;
constructor(private carService: CarsService) {
// this.car = this.carService.getCars();
}
ngOnInit() {
this.carService.getCars().subscribe((carData) => this.car = carData);
}
}