您好,我需要帮助,我创建了一个角度服务,但是当我想查看 json 文件中的数据时,它向我显示了这个错误,我尝试了很多不成功的解决方案
应用程序组件.ts
import {Component, OnInit} from '@angular/core';
import {Car} from './domain/car';
import {CarService} from './service/carservice';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [CarService]
})
export class AppComponent implements OnInit {
cars1: Car[];
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getCarsSmall().subscribe(cars => this.cars1 = cars);
}
}
Run Code Online (Sandbox Code Playgroud)
汽车服务.ts
@Injectable()
export class CarService {
private jsonUrl = './cars-smalll.json';
constructor(private http: Http) {}
getCarsSmall(): Observable<Car[]> {
return this.http.get(this.jsonUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return …Run Code Online (Sandbox Code Playgroud)