Angular 2方法不订阅observable

我正在尝试使用Service类获取JSON数据 . Service class

import { Injectable }     from '@angular/core';
import { Http, HttpModule, Response } from '@angular/http';
import { GeneralTab }     from './generalTab';
import 'rxjs/Rx';
import { Observable }     from 'rxjs/Observable';


@Injectable()
export class GeneralTabService {
     constructor(private _http : Http) {

        console.log("Http call");
    }

   getStatus(): Observable<any>  {
    return      this._http.get('http://samd.server.lan.at/taskmanagement/rest/taskconfigs/IND?language=EN&access_token=200')
         .map((res:Response) => <GeneralTab[]>res.json())
         .do(data => console.log("All: " +  JSON.stringify(data)))
         .catch(this.handleError);

}
  private handleError (error: any) {
    
    let errMsg = (error.message) ? error.message :
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        return Observable.throw(errMsg);
  }
}

Component class

import { Component, OnInit } from '@angular/core';
import {GeneralTab} from "./generalTab";
import {GeneralTabService} from "./generalTab.service";
import { Observable } from 'rxjs/Observable';

    @Component({
        selector: 'general-tab',
        templateUrl: '/general.component.html',
        providers : [GeneralTabService]

    })

    export class GeneralComponent implements OnInit{
        title = 'Serving data from General Component';

        errorMessage: any;
        status: GeneralTab [];
        mode = 'Observable';

        constructor (private generalService: GeneralTabService) {
            this.status = [];
        }

        ngOnInit() {
            console.log("Started");
            this.generalService.getStatus().subscribe(
                (status: any) => this.status = status,
                (error: any) =>  this.errorMessage = error
            );
            console.log(this.status);
        }

    }

GeneralTab class

export class GeneralTab {

    constructor(public recipientId : string,
    public recipientName: string,
    public recipientFullname: string,
    public ouId:String,
    public ouName:String,
    public institute:number,
    public shortName:String,
    public status:String)
    {
    }
}

我在控制台中看到 .do(data => console.log("All:" + JSON.stringify(data))) 正在获取JSON数据

{“subjectConfig”:[{“subject”:“客户数据维护”,“预选”:false,“initialDueDate”:“2016-11-24”,“actionConfigs”:[{“action”:“SEND”,“收件人 “:[{” 用户 “:{” recipientId “:” BD27A4F5923FCA13" , “RECIPIENTNAME”: “ABTABT”, “recipientFullname”: “ABTABT”, “OUID”: “BD27A4F5923FCA13”, “ouName”: “0015”, ....

订阅不会将任何数据分配给状态数组,而我将获得空白的“状态”数组 . 我想将此数据作为状态变量中的数组用于测试目的 . 如何得到?

回答(1)

2 years ago

如果你记录响应就像

console.log("Started");
 this.generalService.getStatus().subscribe(
         (status: any) => this.status = status,
         (error: any) =>  this.errorMessage = error
 );
 console.log(this.status);

当然,由于上面的代码( getStatus().subscribe )是异步的,因此您将看到空白状态 . 这意味着您正在发出请求,然后您正在等待 subscribe 内的响应,当它到来时,您将 status 分配给 this.status . 等待时您已经记录( this.status ),该记录最初是空白的 .

而是尝试这个并检查日志,

console.log("Started");
 this.generalService.getStatus().subscribe(
       (status: any) => {
            this.status = status;
            console.log(this.status);
       },
       (error: any) =>  this.errorMessage = error
 );