Angular 2 http get not getting

后端 未结 4 1592
心在旅途
心在旅途 2020-11-22 06:22

I new to Angular 2 still learning I am trying to hit a URL with a get call but the get doesn\'t seem to go through even in browser\'s network I cannot find that get URL bein

4条回答
  •  北海茫月
    2020-11-22 07:03

    As mentioned by Milad in his answer, since Observables returned by Http's methods are cold/lazy and won't fire until you subscribe to them.

    But let's say what if you don't want to .subscribe to the Observable but still want the HTTP request to fire?

    In case you're using Angular 6 with Rxjs6 and don't want to subscribe , you can do the following:

    ...
    import { publish } from 'rxjs/operators';
    import { HttpClient } from '@angular/common/http';
    
    constructor(private http: HttpClient) {}
    
    getAllPersons() {
      const getAllPersons$ = this.http.get(`https://swapi.co/api/people/1`)
        .pipe(
            publish()
          );
    
      getAllPersons$.connect();
    }
    

    Here's a Sample StackBlitz for your ref.

提交回复
热议问题