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
1. Log in to MySQL database mysql -u root -p View...
Table of contents Three steps to operate the data...
Write a SQL first SELECT DISTINCT from_id FROM co...
win10 + Ubuntu 20.04 LTS dual system installation...
Use div to create a mask or simulate a pop-up wind...
The use of computed in vue3. Since vue3 is compat...
Pseudo-elements and pseudo-classes Speaking of th...
Table of contents Deploy tomcat 1. Download and d...
Content 1. Give readers a reason to stay. Make the...
MySQL Query Cache is on by default. To some exten...
Two problems that are easy to encounter when inst...
When configuring proxy_pass in nginx, if you matc...
Table of contents Question: 1. First attempt 2. R...
Preface If CSS is the basic skill of front-end de...
The process of installing MySQL database and conf...