Home >Web Front-end >JS Tutorial >Detailed explanation of the steps to introduce vue-cli and configure axios

Detailed explanation of the steps to introduce vue-cli and configure axios

php中世界最好的语言
php中世界最好的语言Original
2018-05-23 10:41:294347browse

This time I will bring you a detailed explanation of the steps for introducing vue-cli and configuring axios. What are the precautions for introducing vue-cli and configuring 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) 
    }) 
   }    
}

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好像不需要了,已经自带)

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

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;
 }
}

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

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:

Summary of the use of common algorithms such as JS accumulation, iteration, exhaustion, recursion, etc.

React Family Bucket Environment setup code analysis

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

Statement:
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