Instalación
Código fuente disponible aquí.
Servidor
cd servidor npm install node app.js
Cliente
cd cliente npm install ng serve -o
Peticiones web desde Angular 7
Archivo services/books.service.ts
... import { HttpClient } from '@angular/common/http'; ... export class BooksService { list:any[]; constructor(public http:HttpClient) { } get() { this.http.get("http://localhost:8080/libros/").subscribe((data:any) => { this.list = data; console.log(this.list); }); } add(book:any) { this.http.post("http://localhost:8080/libros/", book).subscribe((data:any) => { this.get(); }); } }
Archivo pages/home/home.component.ts
... import { BooksService } from '../../services/books.service'; ... export class HomeComponent implements OnInit { constructor(public books:BooksService) { } ngOnInit() { this.books.get(); } }
Archivo pages/home/list.component.ts
... import { BooksService } from '../../services/books.service'; ... export class ListComponent implements OnInit { search:string = ''; constructor(public books:BooksService) { } ngOnInit() { this.books.get(); } }
Archivo pages/add/add.component.ts
... import { BooksService } from '../../services/books.service'; ... export class AddComponent implements OnInit { book:any = {id:0, titulo:'', autor:'', precio:0, img:'', url:''}; constructor(public books:BooksService) { } ngOnInit() { } add() { console.log(this.book); this.books.add(this.book); } }