Angular 8 HttpClient呼叫发布

时间:2019-10-05 08:46:02

标签: angular post httpclient

我应该使用angular的httpclient模块进行后期调用,但是在这种情况下,调用没有执行,应该怎么办?

app.component.html:

<div class="col-xs-12">
   <button class="btn btn-primary" (click)="getData()">Chiamata rest</button>
</div>

app.component.ts:

import { Injectable,Component, OnInit ,DoCheck} from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';


const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

getData(){
    const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
    return this.http.post('http://localhost:8787/utente/nuovoutente', body, httpOptions);
  }

1 个答案:

答案 0 :(得分:0)

在实际解决此问题之前,您需要做的事情很少。您应该将要调用api的部分移到有角服务。

如果要执行呼叫,则必须订阅该呼叫。请参阅HttpClient文档。

如果您只想在组件中调用API,请将代码更改为

getData(){
    const body = JSON.stringify({firstName: 'Joele', lastName: 'Smith4'});
    this.http.post('http://localhost:8787/utente/nuovoutente', body, httpOptions).subscribe((data) =>{
       console.log(data);
    });
};