1.创建/public/db.json
[{ "id": 1, "name": "cc" },{ "id": 2, "name": "dd" },{ "id": 3, "name": "hh" }]
2.创建utils/request.js
import axios from 'axios';
// axios.get('/db.json').then((response) => {
// const data = response.data
// console.log(data)
// })
const request = axios.create({
// baseURL: '/dev-api',
baseURL: '/',
timeout: 5000, //请求超时,5000毫秒
});
// 请求拦截器
request.interceptors.request.use(
(config) => {
// 请求拦截
return config;
},
(error) => {
return Promise.reject(error);
}
);
// 响应拦截器
request.interceptors.response.use(
(response) => {
// 响应拦截
return response;
},
(error) => {
return Promise.reject(error);
}
);
// http://localhost:8888/dev-api/db.json 404 (Not Found)
request.get('/db.json').then((response) => {
const data = response.data;
console.log(data);
});
export default request;
3.测试
import request from '../utils/request.js';
4.效果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)