小编Cec*_*cep的帖子

如何使用返回http Promise的Angular 2服务

我在这里遇到角度2的麻烦。我使用返回承诺的服务,但是当我尝试检索响应时出现错误。

我读过这个问题, 这是我的代码。

这是HotelService.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

//rxjs promises cause angular http return observable natively.
import 'rxjs/add/operator/toPromise';

@Injectable()
export class HotelService {

    private BASEURL : any = 'http://localhost:8080/hotel/';

    constructor(private http: Http) {}  

    load(): Promise<any> {
        return this.http.get(this.BASEURL + 'api/client/hotel/load')
            .toPromise()
            .then(response => {
                response.json();
                //console.log(response.json());
            })
            .catch(err => err);
    }
}
Run Code Online (Sandbox Code Playgroud)

此Hotel.ts(组件)

import { Component, OnInit } from '@angular/core';
import { NavController } from 'ionic-angular';

import { HotelService } from …
Run Code Online (Sandbox Code Playgroud)

javascript undefined angular-promise ionic2 angular

5
推荐指数
1
解决办法
4783
查看次数

标签 统计

angular ×1

angular-promise ×1

ionic2 ×1

javascript ×1

undefined ×1