1. Download the axios plugin cnpm install axios -S 2. Introduce axios in main.js import axios from 'axios' Vue.prototype.$http = axios 3. Create an axios instance let service = axios.create({ baseURL: baseUrl, // url = base api url + request url withCredentials: true, // send cookies when cross-domain requests timeout: 5000 // request timeout }) 4. Request Interception let loading; // Request interception service.interceptors.request.use(config => { // start the loding animation loading = Toast.loading({ duration:10000, message: "Loading...", forbidClick:true, }) //config.headers['Authorization'] = sessionStorage.getItem('token') return config },error =>{ console.log(error); return Promise.reject(error) }) 5. Response Interception // Response interception service.interceptors.response.use(res =>{ // Clear loading loading.clear() return Promise.resolve(res) },error =>{ loading.clear() console.log('err'+error); return Promise.reject(error) }) 6. Throw // Throw export default service Complete code // Import fileimport axios from 'axios' import {baseUrl} from '@/config' import {Toast} from 'vant' // Basic configuration let service = axios.create({ baseURL: baseUrl, // url = base api url + request url withCredentials: true, // send cookies when cross-domain requests timeout: 5000 // request timeout }) let loading; // Request interception service.interceptors.request.use(config => { // start the loding animation loading = Toast.loading({ duration:10000, message: "Loading...", forbidClick:true, }) //config.headers['Authorization'] = sessionStorage.getItem('token') return config },error =>{ console.log(error); return Promise.reject(error) }) // Response interception service.interceptors.response.use(res =>{ // Clear loading loading.clear() return Promise.resolve(res) },error =>{ loading.clear() console.log('err'+error); return Promise.reject(error) }) // Throw export default service This is the end of this article about the detailed explanation of JavaScript axios installation and packaging cases. For more relevant js axios installation and packaging content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solve the installation problem of mysql8.0.19 winx64 version
>>: Download and install VSCode on Linux and use programming to output the current time
To execute a shell command in Docker, you need to...
There are too many articles about xhtml+css websi...
Article Structure 1. Preparation 2. Install Java ...
Recently, I participated in the development of th...
Table of contents 1. Introduction to Linux system...
Table of contents MySQL's current_timestamp p...
*******************Introduction to HTML language (...
In daily work, we often need to view logs. For ex...
Table of contents 1. How is cross-domain formed? ...
Table of contents 1. Introduction 2. Use 1. @Comp...
Table of contents How to flatten an array 1. Usin...
Effect Preview Ideas Scroll the current list to t...
Table of contents Preface 1. With vue-cli 1. Defi...
JSON (JavaScript Object Notation, JS Object Notat...
Quoting Baidu's explanation of pseudo-static:...