服务 `http` get 方法没有 return 成功或错误
Service `http` get method doesn't return success or error
我正在编写 return 一系列汽车的服务。我的服务方法没有 return 成功或错误。文件夹 assets
与 `app 文件夹位于同一目录中。
这是我的服务方式:
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Car} from '../../../domain/car';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class CarService {
constructor(private http: Http) {
}
getCarsMedium(): Promise<Car[]> {
console.log("call1");
return this.http.get('assets/data/cars-medium.json')
.toPromise()
.then(res => {
console.log("sucess1" + res);
console.log("sucess2" + res.json());
return res.json().data as Car[]
})
.catch(error => {
console.log("error");
console.error(error);
});
}
}
我称之为:
import {Component, OnInit} from '@angular/core';
import {Car} from "../../../domain/car";
import {CarService} from "./car.service";
@Component({
moduleId: module.id,
templateUrl: './car.component.html',
styleUrls: ['./car.component.css']
})
export class CarComponent implements OnInit {
cars: Car[];
constructor(private carService: CarService) {
}
ngOnInit() {
console.log("call2");
this.carService.getCarsMedium().then(function (cars) {
cars => this.cars = cars;
console.log(cars)
});
}
}
这是我的 json
的样子:
{"data":[
{"vin":"a1653d4d","year":1998, "brand":"VW","color":"White"},
{"vin":"a1653d4w","year":1999, "brand":"VV","color":"White"}
]}
实体class:
export interface Car {
vin?;
year?;
brand?;
color?;
}
编辑:
getCarsMedium() 没有 return 成功或错误。
控制台输出:
通话2
call1
return this.http.get('assets/data/cars-medium.json')
.toPromise()
.then(res => res.json().data as Car[],
res => console.log(res));
显然,这不会打印任何内容,因为您正在打印传递给 then() 的第二个回调的响应,在承诺被拒绝(事实并非如此)的情况下执行。
改为这样做:
return this.http.get('assets/data/cars-medium.json')
.toPromise()
.then(res => {
console.log(res);
console.log(res.json());
return res.json().data as Car[]
});
找到原因了:项目中有jwt-authentication
http://jasonwatmore.com/post/2016/08/16/angular-2-jwt-authentication-example-tutorial][1] - 它阻止了我的请求
我正在编写 return 一系列汽车的服务。我的服务方法没有 return 成功或错误。文件夹 assets
与 `app 文件夹位于同一目录中。
这是我的服务方式:
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Car} from '../../../domain/car';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class CarService {
constructor(private http: Http) {
}
getCarsMedium(): Promise<Car[]> {
console.log("call1");
return this.http.get('assets/data/cars-medium.json')
.toPromise()
.then(res => {
console.log("sucess1" + res);
console.log("sucess2" + res.json());
return res.json().data as Car[]
})
.catch(error => {
console.log("error");
console.error(error);
});
}
}
我称之为:
import {Component, OnInit} from '@angular/core';
import {Car} from "../../../domain/car";
import {CarService} from "./car.service";
@Component({
moduleId: module.id,
templateUrl: './car.component.html',
styleUrls: ['./car.component.css']
})
export class CarComponent implements OnInit {
cars: Car[];
constructor(private carService: CarService) {
}
ngOnInit() {
console.log("call2");
this.carService.getCarsMedium().then(function (cars) {
cars => this.cars = cars;
console.log(cars)
});
}
}
这是我的 json
的样子:
{"data":[
{"vin":"a1653d4d","year":1998, "brand":"VW","color":"White"},
{"vin":"a1653d4w","year":1999, "brand":"VV","color":"White"}
]}
实体class:
export interface Car {
vin?;
year?;
brand?;
color?;
}
编辑: getCarsMedium() 没有 return 成功或错误。 控制台输出: 通话2 call1
return this.http.get('assets/data/cars-medium.json')
.toPromise()
.then(res => res.json().data as Car[],
res => console.log(res));
显然,这不会打印任何内容,因为您正在打印传递给 then() 的第二个回调的响应,在承诺被拒绝(事实并非如此)的情况下执行。
改为这样做:
return this.http.get('assets/data/cars-medium.json')
.toPromise()
.then(res => {
console.log(res);
console.log(res.json());
return res.json().data as Car[]
});
找到原因了:项目中有jwt-authentication http://jasonwatmore.com/post/2016/08/16/angular-2-jwt-authentication-example-tutorial][1] - 它阻止了我的请求