简体   繁体   中英

rxjs simple do http get does nothing

I have code:

export class Test {

constructor(private http: Http) {}

logResponse(): Observable<any> {
  return this.http.get('http://google.com')
    .do(data => console.log("All: " +  JSON.stringify(data)));
  }
}

But still, nothing gets printed out to the console sadly. I am just starting with Rxjs, so sorry for the lame question. Why is it not logged ? I have all the http and imports needed, no exception just nothing gets printed. I trigger it on a button press.

You have to add first subscribe to activate that. This observable is cold ( hot vs cold observables ).

So in your case:

logResponse(): Observable<any> {
  return this.http.get('http://google.com')
    .do(data => console.log("All: " +  JSON.stringify(data)))
    .subscribe(data => console.log("All from subscribe: " +  JSON.stringify(data)));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM