App libros Angular 7 + Bootstrap 4 + NodeJS

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

Publicaciones Similares

  • Angular 1

    AngularJS es un Framework desarrollado con JavaScript AngularJS amplía la funcionalidad en el código HTML La directiva ng-app crea un elemento que contendrá el código web desarrollado con AngularJS. La directiva ng-model enlaza el valor de controles HTML (input, select, textarea) con datos de la página web. La directiva ng-bind enlaza datos de la página…

  • Angular 7

    Installation Angular 7 Components Angular 7 Templating app.component.html nav.component.html nav.component.ts /src/styles.scss nav/component.scss Angular 7 Routing /src/app/app-routing.module.ts Angular 7 Event Binding /src/app/home/home.component.html  home.component.ts Angular 7 Class & Style Binding home.component.html home.component.ts home.component.scss home.component.html home.component.scss home.component.html Angular 7 Services /src/app/data.service.ts /src/app/home/home.component.ts Angular 7 HTTP Client /src/app/app.module.ts /src/app/data.service.ts home.component.ts home.component.html home.component.scss Angular 7 Forms app.module.ts  contact.component.ts contact.component.html contact.component.scss