订阅Observable值

Ann*_*nna 4 observable typescript angular

当我在订阅中记录我的变量的值时,我有一个值但是在外面做它时我得到未定义的???:

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

我想用一定的值初始化我的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, error=> console.error('Error: ' + error), ()=> console.log('finish! ActionsV ngOnInit' + this.page));
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 5

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

console.log()外面subscribe()的呼叫服务器之前执行甚至而成.