1.在根目录下创建一个api目录并创建一个api.js文件
import store from '@/store/store.js'
//apiUrl为域名 url请求接口
async function get(url,data){
let res = await uni.request({
url:store.state.apiUrl+url,
data:data,
method:'GET',
header:{'Authorization':uni.getStorageSync('token')},
})
return res[1].data;
}
async function post(url,data){
let res = await uni.request({
url:store.state.apiUrl+url,
data:data,
method:'POST',
header:{'Authorization':uni.getStorageSync('token')},
})
return res[1].data;
}
async function del(url,data){
let res = await uni.request({
url:store.state.apiUrl+url,
data:data,
method:'DELETE',
header:{'Authorization':uni.getStorageSync('token')}
})
return res[1].data;
}
async function put(url,data){
let res = await uni.request({
url:store.state.apiUrl+url,
data:data,
method:'PUT',
header:{'Authorization':uni.getStorageSync('token')}
})
return res[1].data;
}
export default {
get,
post,
del,
put
}
*2.*在api目录下新建一个index.js文件(这个文件主要放首页相关接口)
//引入api.js
import api from './api.js'
// 首页-轮播图
async function swiper(data){
return await api.get('com/sliding/sliding',data);
}
//导出
export default {
cartList
}
3.在main.js中引入
// 首页接口注入为全局
import index from '@/api/index.js'
Vue.prototype.$index = index
4.在页面上使用
getSwiper(){
//需要传给后台的数据
let data = {
type:0
}
//请求接口
this.$index.swiper(data).then(res=>{
console.log(res)
})
},