axios封装
方法一:
新建api.js
import axios from 'axios';
const instance = axios.create({
baseURL: 'http://xxx/api', // 设置接口基本地址
});
export default {
get(url) {
return new Promise((resolve, reject) => {
instance.get(url).then(response => {
resolve(response);
}).catch(error => {
reject(error);
});
});
},
post(url, data) {
return new Promise((resolve, reject) => {
instance.post(url, data).then(response => {
resolve(response);
}).catch(error => {
reject(error);
});
});
}
};
在页面中使用(home.vue)
import api from './api.js';
export default {
name: 'Home',
data() {
return {
name: '首页&