1.创建request.js,录入一下代码
import axios from 'axios'
const instance = axios.create({
baseURL: 'http://localhost:8888/',
timeout: 30000,
});
// 请求拦截器
instance.request.use(function (config) {
return config;
}, function (error) {
return Promise.reject(error);
});
// 响应拦截器
instance.response.use(function (response) {
return response;
}, function (error) {
return Promise.reject(error);
});
export default instance;
2.创建xxxApi.js文件,根据一下模板录入接口调用
import request from '../request'
export const test = (params) => request.get('/test', { params });
export const hello = (data) => request.post('/hello', data);
3.调用接口
import { test, hello } from "./api/userApi.js";
export default {
methods: {
test() {
test(this.params)
.then((resp) => {
console.log(resp);
})
.catch((err) => {
console.log(err);
});
},
hello() {
hello(this.data)
.then((resp) => {
console.log(resp);
})
.catch((err) => {
console.log(err);
});
},
},
};