1. 安装axios yarn add axios2.建立axios服务import { Injectable } from '@angular/core';import axios from 'axios'import { Observable } from 'rxjs'axios.defaults.timeout = 5000;import { environment } from '../../environments/environment'console.log(environment.baseURL);axios.defaults.baseURL = environment.baseURL@Injectable({ providedIn: 'root'})export class AxiosService { constructor() { } postfun(url:string,params:any){ return axios.post(url,params) .then(response=>{ if(response.status == 200){ Promise.resolve(response.data) } }).catch(err=>{ }) } getfun(url:string,params={}){ return new Promise((resolve,reject)=>{ axios.get(url,{ params:params }) .then(res=>{ resolve(res.data) }) }) } //RXJS进行数据请求 getrxjsdata(){ let url:string = 'http://a.itying.com/api/productlist' return new Observable((observer)=>{ axios.get(url).then(res=>{ observer.next(res) }) }) }}3.引入服务import { AxiosService } from '../../service/axios.service'4.注册constructor(public axios:AxiosService) { }5.创建数据请求函数 getInof(){ let url:string = 'http://a.itying.com/api/productlist' this.axios.getfun(url,{name:'alci'}).then((res:any)=>{ console.log(res.result); }) } //在去调用即可调取函数 5-2或者使用rxjs getInof(){ this.axios.getrxjsdata().subscribe((res:any)=>{ console.log(res.data.result); }) } //再去调用此函数即可