Vue2 installs axios basic configuration

install axios

npm install axios --save

Introducing axios

It can be directly introduced in main.js

You can also recreate the .js file and then introduce the newly created file in main.js

Configure the basic configuration of axiox in the new .js file

import Vue from 'vue'
import axios from 'axios'
axios.defaults.baseURL = '接口路径'
// 请求拦截器--所有通过axios发送的请求,全部都先处理:在请求头中添加
axios.interceptors.request.use(req => {
    
    
  req.headers.Authorization = sessionStorage.getItem('token')
  return req
})
// 响应了拦截器(在响应之后对数据进行一些处理)
axios.interceptors.response.use(res => res.data)
Vue.prototype.$http = axios

The basic configuration is complete.

Supongo que te gusta

Origin blog.csdn.net/qq_45372417/article/details/128137190
Recomendado
Clasificación