vue中api封装
1.封装路径
//封装请求地址路径
//价格管理
let applicantList = `/api/api/GY_prices/getlist`
//价格列表
let applicantreview= `/api/api/GY_prices/getdetails`
export{applicantList,applicantreview}
2.封装方法
import axios from "axios"
//封装请求 get post put
// get(路径,参数)
let get = function(url,parmas){
return new Promise((resolve,reject)=>{
axios({
method: 'Get',
url: url,
parmas:parmas
})
.then(res=>{
resolve(res)
})
.catch(err=>{
reject(err)
});
})
}
let post = function(url,data){
return new Promise((resolve,reject)=>{
axios({
method: 'Post',
url: url,
data:data
})
.then(res=>{
resolve(res)
})
.catch(err=>{
reject(err)
});
})
}
let put = function(url,data){
return new Promise((resolve,reject)=>{
axios.put(url, data).then(res => {
resolve(res.data)
}).catch(err => {
reject(err)
})
})
}
export {get,post,put}
3.调用
import {get,put} from '../../api/api'
import {applicantList,applicantreview} from '../../api/request'
data(){
return{
tableData: [
// {
// username: 'Sheldon Lee Cooper',
// phone:'21334354',
// email:'shoofjjioj@163.com',
// audit_status:'0',
// update_time:'2021-07-21-2131'
// }
],
}
},
mounted(){
this.getPage()
},
methods:{
// 分页
getPage(){
let url = applicantList+'?pageIndex='+this.pageIndex+'&pageSize='+this.pageSize
get(url).then(res=>{
console.log(res)
this.tableData = res.data.data
this.total = res.data.count
})
},
}