0

Does Observable have am array of Books or just one object of books?

 allBooks$: Observable<Books[]>
    
    getBooks() {
      this.allBooks$ = this.bookService.getBooksFromStore();
    }
    
    ngOnInit() {
     this.getBooks();
    }
wolf
  • 146
  • 2
  • 15
  • check this answer https://stackoverflow.com/a/51525059/2312387 – Sherif Abdelkhaliq Apr 17 '20 at 17:56
  • 1
    It means that `getBooks()` function is going to return an [`Observable`](https://angular.io/guide/observables) where each item in the `Observable` is `Books[]`, meaning each item in the observable is going to be an array of `Books` objects. – palaѕн Apr 17 '20 at 18:02
  • Thanks palaѕн now i got it – wolf Apr 17 '20 at 18:06

2 Answers2

1

I think this will help you.

getBooksFromStore(): Observable<Books[]> {
 return this.http.get<Books[]>(this.bookUrl);
}

The above method will return Observable<Books[]>.

Step-2: In our component we will create a property.

allBooks$: Observable<Books[]>

getBooks() {
  this.allBooks$ = this.bookService.getBooksFromStore();
}

ngOnInit() {
 this.getBooks();
}

Also you can refer this : https://www.concretepage.com/angular-2/angular-observable-example

Rohit Tagadiya
  • 3,373
  • 1
  • 25
  • 25
0

//In Service you will configure API call:

getBooksFromStore(): Observable < Books[] > {
  return this.http.get < Books[] > (this.bookUrl);
}

//In Component (you will have to subscribe to the Observable same as you do using 'then' in Promises):

ngOnInit() {
  this.bookService.getBooksFromStore().subscribe({
    next: (response) => {
      this.books = response;
    }
  });
}