Home > Web Front-end > JS Tutorial > How to use vue-cli to introduce and configure axios

How to use vue-cli to introduce and configure axios

php中世界最好的语言
Release: 2018-05-31 09:37:43
Original
1299 people have browsed it

This time I will show you how to use vue-cli to introduce and configure axios, and what are the precautions for using vue-cli to introduce and configure axios. The following is a practical case, let's take a look.

1. npm install axios, install it in the file root directory, the instructions are as follows

npm install axios --save-dev

2. Modify the prototype Chain, introduce axios in main.js

import axios from 'axios'

Then rewrite axios as the prototype attribute of Vue,

Vue.prototype.$http=axios

In this way, the $http command can be called in the methods of each component to complete the data request

3. Use in the component

methods: { 
   get(){ 
    this.$http({ 
     method:'get', 
     url:'/url', 
     data:{} 
    }).then(function(res){ 
     console.log(res) 
    }).catch(function(err){ 
     console.log(err) 
    }) 
     
    this.$http.get('/url').then(function(res){ 
     console.log(res) 
    }).catch(function(err){ 
     console.log(err) 
    }) 
   }    
}
Copy after login

For the configuration of axios, please refer to the following document, click to open the link

The following will introduce how to configure axios with vue-cli

1.

npm install axios --save

2.

npm install @type/axios --save-dev(使用ts编写的需要此声明文件,升级的axios好像不需要了,已经自带)
Copy after login

3.

In the src directory Add the axios.ts file below, content:

import axios from 'axios'
import {Notification} from 'element-ui'
import store from './store/index'
import buildconf from '../config/build.rootpath.js'
axios.defaults.withCredentials = true;
axios.defaults.baseURL = buildconf.serverUrl
// axios.defaults.baseURL = 'http://gsblackwidow.chinacloudsites.cn/'
axios.interceptors.request.use(function(config) {
 // document.getElementById('g-loader').style.display = 'flex'
 store.commit('requestModify', 1)
 return config;
}, function(error){
 return Promise.reject(error)
})
axios.interceptors.response.use(function(response){
 store.commit('requestModify', -1)
 // document.getElementById('g-loader').style.display = 'none' 
 return response.data;
}, function(error){
 store.commit('requestModify', -1) 
 // document.getElementById('g-loader').style.display = 'none'  
 if(error.response.status === 401){
  Notification({
   title: '权限无效',
   message: '您的用户信息已经失效, 请重新登录',
   type: 'warning',
   offset: 48
  });
  window.location.href = '/#/login'
 }else{
  Notification({
   title: '请求错误',
   message: `${error.response.status} \n ${error.config.url}`,
   type: 'error',
   offset: 48,
  })
 }
 return Promise.reject(error)
})
export default axios
Copy after login

4.

Create a new vue.d.ts file in the types folder, content:

import {AxiosStatic, AxiosInstance } from 'axios'
declare module 'vue/types/vue' {
 interface Vue {
  $axios: AxiosStatic;
 }
}
Copy after login

This way you can Axios is used in the module through this.$axios

Among them, axios:

1. Build.rootpath.js content:

var path = require('path')
var rootpath = path.resolve(dirname, '../dist')
module.exports = rootpath
Copy after login

2. Store is a vuex file , so you need to install vuex in advance

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use vux-ui custom form verification

How to use routing guards within Angular routing

The above is the detailed content of How to use vue-cli to introduce and configure axios. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template