vue-cli 引入、配置axios的方法
一、npm 安裝axios,文件根目錄下安裝,指令如下
npm install axios --save-dev
二、修改原型鏈,在main.js中引入axios
import axios from 'axios'
接著將axios改寫為Vue的原型屬性,
Vue.prototype.$http=axios
這樣之后就可在每個(gè)組件的methods中調(diào)用$http命令完成數(shù)據(jù)請求
三、在組件中使用
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)
})
}
}
有關(guān)axios的配置請參考如下文檔,點(diǎn)擊打開鏈接
下面給大家介紹下vue-cli配置axios的方法
1.
npm install axios --save
2.
npm install @type/axios --save-dev(使用ts編寫的需要此聲明文件,升級的axios好像不需要了,已經(jīng)自帶)
3.
在src目錄下添加axios.ts文件,內(nèi)容:
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: '權(quán)限無效',
message: '您的用戶信息已經(jīng)失效, 請重新登錄',
type: 'warning',
offset: 48
});
window.location.href = '/#/login'
}else{
Notification({
title: '請求錯(cuò)誤',
message: `${error.response.status} \n ${error.config.url}`,
type: 'error',
offset: 48,
})
}
return Promise.reject(error)
})
export default axios
4.
types文件夾中新建vue.d.ts文件,內(nèi)容:
import {AxiosStatic, AxiosInstance } from 'axios'
declare module 'vue/types/vue' {
interface Vue {
$axios: AxiosStatic;
}
}
這樣就可以在各個(gè)模塊中通過this.$axios來使用axios了
其中axios中:
1. build.rootpath.js內(nèi)容:
var path = require('path')
var rootpath = path.resolve(__dirname, '../dist')
module.exports = rootpath
2. store是vuex的文件,所以要事先安裝vuex
總結(jié)
以上所述是小編給大家介紹的vue-cli 引入、配置axios的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue定時(shí)器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題
這篇文章主要介紹了vue定時(shí)器清除不掉,導(dǎo)致功能頻繁執(zhí)行問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue3使用hooks解決字典數(shù)據(jù)的顯示問題
我們在使用 element-plus的時(shí)候,經(jīng)常會(huì)使用一些字典數(shù)據(jù), 在搜索框的時(shí)候,字典數(shù)數(shù)要使用 el-select el-option 來顯示,但是經(jīng)常會(huì)遇到字典數(shù)據(jù)的顯示問題,所以本文給大家介紹了Vue3使用hooks解決字典數(shù)據(jù)的顯示問題,需要的朋友可以參考下2024-12-12
Element-UI控件Tree實(shí)現(xiàn)數(shù)據(jù)樹形結(jié)構(gòu)的方法
這篇文章主要介紹了Element-UI控件Tree實(shí)現(xiàn)數(shù)據(jù)樹形結(jié)構(gòu),本期介紹添加、修改等功能也比較簡單,可以通過element-ui的$prompt彈框控件來實(shí)現(xiàn),需要的朋友可以參考下2024-01-01
解決vue3報(bào)錯(cuò):找不到模塊或其相應(yīng)的類型聲明
這篇文章主要給大家介紹了關(guān)于如何解決vue3報(bào)錯(cuò):找不到模塊或其相應(yīng)的類型聲明的相關(guān)資料,這個(gè)錯(cuò)誤提示是指在代碼中引用了Vue模塊,但是系統(tǒng)找不到該模塊或者缺少相應(yīng)的類型聲明文件,需要的朋友可以參考下2023-07-07
深入理解使用Vue實(shí)現(xiàn)Context-Menu的思考與總結(jié)
這篇文章主要介紹了使用Vue實(shí)現(xiàn)Context-Menu的思考與總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03

