加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

订阅Angular 2

发布时间:2020-12-17 17:54:12 所属栏目:安全 来源:网络整理
导读:我想要我的ngOnInit函数做下一件事: ? – 使用this.structureRequest.sendRequest()对某些数据进行http请求,这样可以正常工作,并且在收到数据后开始使用this.viewNodes()函数查看它. 我使用订阅,但它不起作用,我认为我做订阅功能有问题.请帮忙:) HomeCompo
我想要我的ngOnInit函数做下一件事:
? – 使用this.structureRequest.sendRequest()对某些数据进行http请求,这样可以正常工作,并且在收到数据后开始使用this.viewNodes()函数查看它.
我使用订阅,但它不起作用,我认为我做订阅功能有问题.请帮忙:)

> 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;
 }
 }

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; });
}
}

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

解决方法

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

这个

this.structureRequest.sendRequest().subscribe( this.viewNodes());

应改为

this.structureRequest.sendRequest().subscribe(() => this.viewNodes());

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

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

this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result));

更新

sendReqeust()不返回任何内容.

它应该是

return this.http.get(this.myUrl,options) ...

但是这只能在你的代码中使用它的方式,如果它返回一个Observable.

但最后的subscribe()返回一个订阅

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

因此应该改为

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

要么

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读