订阅Angular 2

Ser*_*hiy 4 subscribe angular

我希望我的ngOnInit函数做下面的事情: - 使用this.structureRequest.sendRequest()对某些数据进行http请求,这很好,并且在收到数据后开始使用this.viewNodes()函数查看它.我使用订阅,但它不起作用,我认为我做订阅功能有问题.请帮忙:)

  1. HomeComponent.ts

    import {Component} from '@angular/core';
    import {Observable} from 'rxjs/Observable';
    import {StructureRequestService} from './StructureRequestService';
    
    
    export class Content {
    
    
    ok: boolean;
    content = [];
    }
    @Component({
    providers: [StructureRequestService],
    styleUrls: ['app/home/home.css'],
    templateUrl:'./app/home/homePageTemplate.html'
    
    })
    
    export class HomeComponent {
    contentArray = [];
    myRes: Content;
    showAssigned:boolean = false;
    showSubitems:boolean = false;
    showUsers:boolean = false;
    
    constructor(private structureRequest: StructureRequestService) {}
    
    ngOnInit() {
    this.structureRequest.sendRequest().subscribe( this.viewNodes());
    
    }
    
    viewNodes() {
    this.myRes = this.structureRequest.result;
    this.contentArray = this.myRes.content;
    this.showAssigned = true;
     }
     }
    
    Run Code Online (Sandbox Code Playgroud)

2.这是http服务,http get工作正常,收到的所有数据:

import {Injectable} from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';

import {Observable} from 'rxjs/Observable';

@Injectable ()
export class StructureRequestService {
result: Object;
//private myUrl = 'http://manny.herokuapp.com/audit/get/structure';
private myUrl = './app/home/nodes.json';  // local URL to structure APi
constructor (private http: Http) {
    //use XHR object
    let _build = (<any> http)._backend._browserXHR.build;
    (<any> http)._backend._browserXHR.build = () => {
        let _xhr =  _build();
        _xhr.withCredentials = true;
        return _xhr;
    };
}

sendRequest() {
    let body = JSON.stringify({});
    let headers = new Headers({ 'Content-Type': 'application/json'});
    let options = new RequestOptions({
        headers: headers
    });
     this.http.get(this.myUrl, options)
        .map((res: Response) => res.json())
        .subscribe(res => {this.result = res;
            return this.result; });
}
}
Run Code Online (Sandbox Code Playgroud)

3.问题是制定同步步骤:接收数据,而不是查看数据.

Gün*_*uer 9

您可能希望this.viewNodes在请求返回值而不执行结果时执行this.viewNodes()

这个

this.structureRequest.sendRequest().subscribe( this.viewNodes());
Run Code Online (Sandbox Code Playgroud)

应改为

this.structureRequest.sendRequest().subscribe(() => this.viewNodes());
Run Code Online (Sandbox Code Playgroud)

前者执行this.viewNodes()并传递结果subscribe(),后者创建一个新的内联函数传递给subscribe().执行此内联函数时,将执行此函数this.viewNodes()

如果你想传递值sendRequest()返回它应该是

this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result));
Run Code Online (Sandbox Code Playgroud)

更新

sendReqeust() 没有任何回报.

它应该是

return this.http.get(this.myUrl, options) ...
Run Code Online (Sandbox Code Playgroud)

但这仅适用于您在代码中使用它的方式,如果它返回一个Observable.

subscribe()最后返回一个Subscription

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .subscribe(res => {this.result = res;
        return this.result; });
Run Code Online (Sandbox Code Playgroud)

因此应该改为

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .map(res => {
      this.result = res;
      return this.result; 
    });
Run Code Online (Sandbox Code Playgroud)

要么

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .do(res => {
      this.result = res;
    });
Run Code Online (Sandbox Code Playgroud)

不同之处在于do()不会修改流的值,也不需要返回任何内容.返回的值.map()将被转发.如果要使用,请确保do已导入(如map).