Open web-dave opened 7 years ago
npm i --save-dev bookmonkey-api
ng g service books/shared/books -m=books/books.module
...
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"rest": "bookmonkey-api" // <---
},
...
npm run rest
GET /books // Get all books GET /books/:isbn // Get a specific book by ISBN POST /books // Create a new book PUT /books/:isbn // Update a book by ISBN DELETE /books/:isbn // Delete a book by ISBN
import { HttpClientModule } from '@angular/common/http';
...
@NgModule({
...
imports: [
... ,
HttpClientModule // <---
],
...
})
export class BooksModule { }
import { HttpClient } from '@angular/common/http';
restRoot = 'http://localhost:4730/books';
constructor(private http: HttpClient) { }
getBooks() {
const url = this.restRoot;
return this.http.get(url);
}
books
service in a shared folder ofBooksModule
HttpClientModule
is imported to yourAppModule
HttpClient
from@angular/common/http
and inject it (DI) into your service