普通的请求
axios({url: \'\',// method: \'post\'}).then(res => {console.log(res);})axios.defaults.baseURL = \"\";axios.defaults.timeout = 2000axios.all([axios({url: \'/home\'}), axios({url: \'/category\',params: {type: \'sell\',page: 5}})]).then(res => {console.log(res[0], res[1]);})
如果组件很多每次都要重复相同的代码 可以创建axios相应的实例
const instance1 = axios.create({baseURL: \'\',timeout: 3000})instance1({url: \'/home\',}).then(res => {console.log(res);})instance1({url: \'/category\',params: {type: \'sell\',page: 5}}).then(res => {console.log(res);})const instance2 = axios.create({baseURL: \'\',timeout: 5000})
建议把请求网络用函数给封装起来 这样每个组件只用调用一次这个函数就好了
把一个函数封装到一个专门的js文件
import axios from \'axios\'export function request(config) {// 1.创建axios实例const instance1 = axios.create({baseURL: \'\',timeout: 5000})// 发送网络请求return instance1(config)
然后在需要的组件里进行调用
import {request} from \'./network/request\'request({url: \'/home\'}).then(res => {console.log(res);}).catch(err => {console.log(err);})