新建 LanBowanShop/utils/request.js
import cofig from './config.js'
export default (url,data={},method='GET')=>{
return new Promise((resolve,reject)=>{
uni.request({
url:cofig.baseUrl+url,
data,
method,
success(res) {
resolve(res.data);
},
fail(err) {
reject(err);
}
})
})
}
新建 LanBowanShop/utils/config.js
export default {
baseUrl: 'http://localhost:7788'
}
测试封装好的请求方法
<script>
//1.导入封装好的请求数据方法
import http from '@/untils/request.js';
export default {
data() {
return {
title: 'Hello'
}
},
methods: {
//2.请求首页数据
async getIndexData() {
let {
data: res
} = await http('/getIndexData');
console.log(res);
}
},
created() {
// 3.在声明周期函数中使用
this.getIndexData()
}
}
</script>