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();
}
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();
}
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
//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;
}
});
}