【请求后台接口】30秒完成Angular10精简版HttpClient请求服务搭建
生活随笔
收集整理的這篇文章主要介紹了
【请求后台接口】30秒完成Angular10精简版HttpClient请求服务搭建
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ng g s services/http
?app.module.ts
...
@NgModule({declarations: [...],imports: [...HttpClientModule,//這個(gè)很重緊要,沒(méi)有就會(huì)報(bào)錯(cuò)],providers: [],bootstrap: [AppComponent]
})
export class AppModule { }
services/http.service.ts?
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';@Injectable({providedIn: 'root'
})
export class HttpService {private apiUrl = "http://localhost:8888/test/1.json";constructor(private http: HttpClient) { }public getData(params: any) {return this.http.get(this.apiUrl, { params });}public postData(params: any) {return this.http.post(this.apiUrl, { params });}}
?app.component.ts
import { HttpService } from './services/http.service';...constructor(public http: HttpService) { }...ngOnInit() {this.http.getData({ param1: 1, param2: 2 }).subscribe(d => {console.log(d);});this.http.postData({ param1: 1, param2: 2 }).subscribe(d => {console.log(d);});}
最終呈現(xiàn)的效果是
?
總結(jié)
以上是生活随笔為你收集整理的【请求后台接口】30秒完成Angular10精简版HttpClient请求服务搭建的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 解决vue项目build之后部署到服务器
- 下一篇: webstorm设置git pull快捷