📌  相关文章
📜  angular add httpclient - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:20.001000             🧑  作者: Mango

代码示例1
import { HttpClientModule } from '@angular/common/http';
// if you use services just import this
import { HttpClient } from '@angular/common/http';
//................................................
@NgModule({
  imports: [
    BrowserModule,
    // import HttpClientModule after BrowserModule.
    HttpClientModule,
  ],
  
  // in you component.ts
  readonly ROOT_URL = "https://jsonplaceholder.typicode.com";
  
 constructor( private http: HttpClient) { }
  
  this.http.get(this.ROOT_URL + '/posts')
    .subscribe((response)=>{
      alert(JSON.stringify(response));
    });

// send information

this.http.post(this.ROOT_URL + '/posts',
              { id: 1, title: "sami"} )
    .subscribe((response)=>{
      alert(JSON.stringify(response));
    });

// update

this.http.put(this.ROOT_URL + '/posts')
    .subscribe((response)=>{
      alert(JSON.stringify(response));
    });

// fetch

// delete