1、在项目根目录打开cmd,输入命令行:
npm init -y
npm install @escook/request-miniprogram
2、在main.js 文件中:
// 按需导入 $http 对象
import {
$http
} from '@escook/request-miniprogram'
// 挂载
uni.$http = $http
// 请求拦截器
$http.beforeRequest = function(options) {
uni.showLoading({
title: '数据加载中'
})
}
// 响应拦截器
$http.afterRequest = function() {
uni.hideLoading()
}
// 请求的根路径
$http.baseUrl = '域名'
3、在使用的页面index.vue:
onLoad() {
this.getData()
},
methods: {
async getData() {
// 请求参数
const params = {
data:{},
meta:{}
}
const data1= await uni.$http.post( ' 域名后的接口地址1 ',params ) // post请求
console.log( data1)
const data2= await uni.$http.get( ' 域名后的接口地址1 ' ) // get请求
console.log( data2 )
}