Detailed explanation of JavaScript axios installation and packaging case

Detailed explanation of JavaScript axios installation and packaging case

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:
  • In-depth analysis of homology and cross-domain, jsonp (function encapsulation), CORS principle
  • Vue.js manages the encapsulation of background table components
  • Detailed explanation of JavaScript object-oriented practice: encapsulation and dragging objects
  • Native js encapsulation seamless carousel function
  • Native JS encapsulation vue Tab switching effect
  • js implements a simple method of encapsulating jQuery and a detailed explanation of chain operations
  • js implements some functions of the input component in Element and encapsulates it into a component (example code)
  • JavaScript implements prototype encapsulation carousel
  • Encapsulation method of JavaScript slow motion animation function
  • JavaScript canvas encapsulation dynamic clock
  • About Jackson's JSON tool class encapsulation JsonUtils usage
  • Example code for JavaScript encapsulation of a singly linked list
  • Common front-end JavaScript method encapsulation

<<:  Solve the installation problem of mysql8.0.19 winx64 version

>>:  Download and install VSCode on Linux and use programming to output the current time

Recommend

How to execute Linux shell commands in Docker

To execute a shell command in Docker, you need to...

Use PS to create an xhtml+css website homepage in two minutes

There are too many articles about xhtml+css websi...

Linux system (Centos6.5 and above) installation jdk tutorial analysis

Article Structure 1. Preparation 2. Install Java ...

WeChat applet development chapter: pitfall record

Recently, I participated in the development of th...

A detailed introduction to Linux system operation levels

Table of contents 1. Introduction to Linux system...

Share the pitfalls of MySQL's current_timestamp and their solutions

Table of contents MySQL's current_timestamp p...

HTML tutorial, easy to learn HTML language (2)

*******************Introduction to HTML language (...

Causes and solutions for cross-domain issues in Ajax requests

Table of contents 1. How is cross-domain formed? ...

How to apply TypeScript classes in Vue projects

Table of contents 1. Introduction 2. Use 1. @Comp...

js implements array flattening

Table of contents How to flatten an array 1. Usin...

CSS3 achieves infinite scrolling/carousel effect of list

Effect Preview Ideas Scroll the current list to t...

Complete example of Vue encapsulating the global toast component

Table of contents Preface 1. With vue-cli 1. Defi...

Best way to replace the key in json object

JSON (JavaScript Object Notation, JS Object Notat...

Example of how to set WordPress pseudo-static in Nginx

Quoting Baidu's explanation of pseudo-static:...