Warm tip: This article is reproduced from stackoverflow.com, please click
angular ionic4 typescript rxjs angular-httpclient

http.get() does not return an observable

发布于 2020-03-27 10:19:05

NOTE: The problem is definetly the ionViewDidLoad() not being executed cause the http.get does get an observable. I'm trying to get the observable when executing a request in order to get its json response later but I don't get an error, and nothing is displayed in the browser's console either. I don't know why it does not return anything.

I have tried many urls and checked many times the imports. console.log not pasting the data home.page.ts


    import { ServicioService } from '../servicio.service';
    import { Observable } from 'rxjs';
    import { HttpClient } from '@angular/common/http';
    ...
    ionViewDidLoad(){
          const hola = this.servicio.goWiki(this.userInput).
          subscribe((response) => {
            console.log(response);
           });
        }

servicio.service.ts


    import { Injectable } from '@angular/core';
    import { map } from 'rxjs/operators';
    import { HttpClient } from '@angular/common/http';
    import { Observable } from 'rxjs';

    @Injectable()
    export class ServicioService {

        constructor(public http: HttpClient) {
            console.log('goin!');
        }

        goWiki(userInput): Observable<any>{
            return this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=105sort=hot');
        }

app.module.ts


    import { ServicioService } from './servicio.service';
    import { HttpClientModule } from '@angular/common/http';

    imports: [HttpClientModule, BrowserModule, IonicModule.forRoot(), AppRoutingModule, ComponentesModule],
      providers: [
        ServicioService,...

I just hope to get an observable to be able to read it and extract specific data from it.

Questioner
Ferran Ramírez Martí
Viewed
96
ghybs 2019-07-04 01:59

With Ionic 4, in Angular you no longer have the ionViewDidLoad lifecycle hook. Use the standard Angular ngOnInit hook instead.

See https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864

Lifecycles in Ionic 4

[...]

Except ionViewDidLoad (because it’s the same like ngOnInit) and the two nav guards all Lifecycle hooks from Ionic 3 are still available