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

typescript – 订阅Observable值

发布时间:2020-12-17 17:50:48 所属栏目:安全 来源:网络整理
导读:当我在订阅中记录我的变量的值时,我有一个值但是在外面做它时我得到未定义的???: this._postService.getConf("app/conf/conf.json") .subscribe(res ={ this.home = JSON.stringify(res); console.log("value :" + this.home); 我想用一定的值初始化我的hom
当我在订阅中记录我的变量的值时,我有一个值但是在外面做它时我得到未定义的???:

this._postService.getConf("app/conf/conf.json")
   .subscribe(res =>{
      this.home = JSON.stringify(res);
      console.log("value :" + this.home);

我想用一定的值初始化我的home变量我在ngOnInit上做它但是当我试图把它弄到外面时它得到值undefined:

这是我的功能:

getConf(url) {
   return this._http.get(url).
      map(res => {return res.json()});
}

ngOnInit() {

  this._postService.getConf("app/conf/conf.json")
   .subscribe(res => {
        this.home = JSON.stringify(res);
        console.log("hahowaaaaaaaa" + this.home);
   }
  );

  this._postService.getPosts(this.home)
  .subscribe(result =>{ 
    this.loading = false;
    this.actionsG = result; 
    var count = JSON.stringify(result);

    this.aCount = count.substring(count.indexOf(','),23);

  },error=> console.error('Error: ' + error),()=> console.log('finish! ActionsG'));

  this._postService.getPosts(this.home ).subscribe(result => this.actionsV = 
    result,()=> console.log('finish! ActionsV ngOnInit' + this.page));
}

解决方法

这就是可观察的工作方式.当来自服务器的响应到达时,传递给subscribe(…)的回调由observable调用.

subscribe()之外的console.log()在甚至进行对服务器的调用之前执行.

(编辑:李大同)

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

    推荐文章
      热点阅读