Я начал работать с API на Angular 8, и я делаю простой запрос get в API-заполнитель JSON с Angular HttpClientModule, и это дает мне следующее:
это app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
и мой app.component.ts
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor(private http: HttpClient){}
readonly PLACEHOLDER_URL = 'https://jsonplaceholder.typicode.com'
posts: any;
title = 'movies';
getPosts() {
this.posts = this.http.get(this.PLACEHOLDER_URL + '/posts')
}
}
и мой app.component.html
<button (click)="getPosts()"></button>
<div *ngIf="let posts of posts | async">{{post | json}}</div>
<router-outlet></router-outlet>