如何在 Angular 2 中显示我的视图之前解决一些 stuff/ajax 调用?

How can resolve some stuff/ajax call before to display my view in Angular 2?

我已经尝试过这个建议,但它不起作用 Observable 没有任何 "fromPromise" 方法,打字稿会出现错误。

import {Observable} from "rxjs/Rx";
import {Http} from "@angular/http";
import {GLOBAL_CONST} from "../global-const";
import {Resolve, ActivatedRouteSnapshot, RouterStateSnapshot} from "@angular/router";

export class ResolverSignupBegin{//} implements Resolve<any>{

    signupBeginData:any[];

    constructor(private http:Http){}

    getData(){
        return this.http.get(GLOBAL_CONST.apiPath + "/user/login/api-logged-in")
            .map(res => res.json())
            .subscribe(
                (data) => { console.log("data", data); return data; },
                (err) => { console.log("error", err); }
            );
    }


    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<any>{
        return this.getData();
    }


}

我在 "this".getData 上有一个错误,说“错误:(28, 16) TS2322:类型 'Subscription' 不可分配给类型 'Observable'。 属性 'Subscription' 类型中缺少“_isScalar”。 “

如果您订阅,您将获得 Subscription,但 resolve 需要 return Observable

与其订阅自己 return 一个可观察对象,不如让 Angular 订阅

getData(){
    return this.http.get(GLOBAL_CONST.apiPath + "/user/login/api-logged-in")
        .map(res => res.json())
        .catch((err) => { console.log("error", err); throw err;}
        .do(
            (data) => { console.log("data", data)},
        );
}

不要忘记您需要导入 mapdo 等运算符。参见