Angular2 Http / Jsonp 不发出请求

Angular2 Http / Jsonp Not Making Request

我正在使用 Angular 2.0.0-beta.16 并尝试从我的 RESTful API 获取数据。我有以下服务:

import {Injectable}     from 'angular2/core';
import {Jsonp, Response, Headers, RequestOptions} from 'angular2/http';
import {Store}          from './store';
import {Observable}     from 'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
export class StoreService {
    constructor(private jsonp: Jsonp) {

    }

    getStores(): Observable<Store[]> {
      console.log("getting stores");
      // let headers = new Headers({ 'Content-Type': 'application/json' });
      // let options = new RequestOptions({ headers: headers });
        return this.jsonp.get("http://localhost:8080/stores")
            .map(this.extractData)
            .catch(this.handleError);
    }

    private extractData(res: Response) {
      console.log(res.status);
        if (res.status < 200 || res.status >= 300) {
            throw new Error('Bad response status: ' + res.status);
        }
        let body = res.json();
        return body.data || {};
    }


    private handleError(error: any) {
        // In a real world app, we might send the error to remote logging infrastructure
        let errMsg = error.message || 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }
}

我正在从我的组件调用 getStores()。我知道它正在进入 getStores() 函数,因为我收到了 console.log 消息。但是,没有其他事情发生。我在 chrome 开发工具中没有看到任何请求。没有错误记录到控制台。根本不值一提。 JsonpHttp 我都试过了,但它们都给出了相同的结果。

您需要订阅 getStores() 返回的 observable。 Observables 是惰性的,没有 subscribe() 或 `toPromise()

什么都不做
getStores().subscribe(val => { console.log(val); };