在request.js文件中写入以下代码:
const baseURL = '/api' //基础地址和manifest.json文件中配置proxy的属性一样
export function request(options = {}) {
return new Promise((resolve, reject) => {
uni.request({
url: baseURL + options.url,
method: options.method,
data: options.data,
header: {
'Authorization': 'Bearer ' + uni.getStorageSync('uniIdToken'), //请求头 + token
},
success: (response) => {
return resolve(response.data)
},
fail: (fail) => {
console.log('fail', fail)
return reject(fail);
}
})
})
}
新建api.js文件引入request文件,按如下格式引入接口:
export const detail =(options)=>{
return request({
url:'/list',
method:'GET',
data:options
})
}
在需要请求的地方引入js文件
<script>
import {detail} from '@/api/api.js'
export default {
methods:{
async getDetails(){
const res = await detail()
}
}
}
</script>