import axios from 'axios'
const qs = require('qs')
const request= {
async get (url, data) {
try {
let res = await axios.get(url, {params: data})
res = res.data
return new Promise((resolve) => {
if (res.code === 0) {
resolve(res)
} else {
resolve(res)
}
})
} catch (err) {
console.error(err)
}
},
async post (url, data) {
try {
let res = await axios.post(url, qs.stringify(data))
res = res.data
return new Promise((resolve, reject) => {
if (res.code === 200 || res.code === 'success') {
resolve(res)
} else {
reject(res)
}
})
} catch (err) {
console.error(err)
}
},
}
export { request }
使用
import { request } from 'common/js/request'
export default {
data () {
return {
list: [],
}
},
mounted() {
this.getList()
},
methods: {
async getList () {
const {data} = await request.get('/test/list')
this.list = data
}
},
}
网友评论