可能是vue中使用axios最詳細(xì)教程
前提條件:vue-cli 項(xiàng)目
安裝:
npm axios from 'axios'
較科學(xué)的封裝好的axios:(new-axios.js)
import?axios?from?'axios' import?{?Notify?}?from?'vant'; //?import?Vue?from?'vue' //?import?store?from?'@/store'? // 我此項(xiàng)目沒有用到vuex,所以vuex代碼的都注釋了,需要的自己打開使用 //?import?{ACCESS_TOKEN}?from?'@/store/mutation-types' //?創(chuàng)建?axios?實(shí)例 const?requests?=?axios.create({ ??baseURL:?process.env.VUE_APP_API,?// 基礎(chǔ)url,如果是多環(huán)境配置這樣寫,也可以像下面一行的寫死。 // baseURL: 'http://168.192.0.123', ??timeout:?6000?//?請(qǐng)求超時(shí)時(shí)間 }) ? // 錯(cuò)誤處理函數(shù) const?err?=?(error)?=>?{ ??if?(error.response)?{ ????const?data?=?error.response.data ????//?const?token?=?Vue.ls.get(ACCESS_TOKEN) ????if?(error.response.status?===?403)?{ ????????Notify({?type:?'danger',?message:?data.message||data.msg?}); ????} ????if?(error.response.status?===?401)?{ ????????Notify({?type:?'danger',?message:?'你沒有權(quán)限。'?}); ??????//?if?(token)?{ ??????//???store.dispatch('Logout').then(()?=>?{ ??????//?????setTimeout(()?=>?{ ??????//???????window.location.reload() ??????//?????},?1500) ??????//???}) ??????//?} ????} ??} ??return?Promise.reject(error) } //?request?interceptor(請(qǐng)求攔截器) requests.interceptors.request.use(config?=>?{ //???const?token?=?Vue.ls.get(ACCESS_TOKEN) ??const?token?=?localStorage.getItem('token') ??if?(token)?{ ????config.headers['token']?=?token?//?讓每個(gè)請(qǐng)求攜帶自定義?token?請(qǐng)根據(jù)實(shí)際情況自行修改 ??} ??return?config },?err) //?response?interceptor(接收攔截器) requests.interceptors.response.use((response)?=>?{ ??const?res?=?response.data ??if?(res.code?!==?0&&res.code!==200)?{? ????Notify({?type:?'danger',?message:?res.message||res.msg?}); ????//?401:未登錄; ????if?(res.code?===?401||res.code?===?403||res.code===999)?{ ??????Notify({?type:?'danger',?message:?'請(qǐng)登錄'}); ????} ????return?Promise.reject('error') ??}?else?{ ????return?res ??} },?err) export?default?{ ??requests }
main.js 引入,添加到vue原型
import requests from '@s/basejs/new-axios.js' // 記得改為你的路徑 Vue.prototype.rq = requests // 此處命名為rq,你可以改
使用
this.rq.get('/api/product/get?productChannelId='+this.productChannelId).then(res=>{ console.log(res) }) // 傳對(duì)象參數(shù) // get請(qǐng)求記得加params this.rq.get('/api/product/get,{params:{name:'abc'}}).then(res=>{ console.log(res) }) this.rq.post('/api/product/get,{name:'abc'}).then(res=>{ console.log(res) })
以下步驟一般不需要
開發(fā)環(huán)境如果要跨域,解決跨域問題(設(shè)置代理):vue-cli 3.0的在 package.json 同級(jí)目錄新建一個(gè) vue.config.js 文件,加入下面代碼,其他版本找到配置文件的devServer加入代碼
:
module.exports = { //axios域代理,解決axios跨域問題 baseUrl: '/', devServer: { proxy: { '': { target: 'http://192.168.0.108:8090', changeOrigin: true, ws: true, pathRewrite: { } } } } }
修改完后記得重啟項(xiàng)目應(yīng)用配置
總結(jié)
到此這篇關(guān)于vue中使用axios最詳細(xì)教程的文章就介紹到這了,更多相關(guān)vue使用axios教程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue組件設(shè)計(jì)-滾動(dòng)置頂設(shè)計(jì)案例
這篇文章主要介紹了Vue組件設(shè)計(jì)-滾動(dòng)置頂設(shè)計(jì)案例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法
下面小編就為大家分享一篇vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03vue項(xiàng)目中使用axios遇到的相對(duì)路徑和絕對(duì)路徑問題
這篇文章主要介紹了vue項(xiàng)目中使用axios遇到的相對(duì)路徑和絕對(duì)路徑問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Vue3警告:Failed to resolve component:XXX的詳細(xì)解決辦法
最近在一個(gè)vue3項(xiàng)目中遇到了報(bào)錯(cuò),整理了些解決辦法,這篇文章主要給大家介紹了關(guān)于Vue3警告:Failed to resolve component:XXX的詳細(xì)解決辦法,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05vue大型項(xiàng)目之分模塊運(yùn)行/打包的實(shí)現(xiàn)
這篇文章主要介紹了vue大型項(xiàng)目之分模塊運(yùn)行/打包的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Element UI框架中巧用樹選擇器的實(shí)現(xiàn)
這篇文章主要介紹了Element UI框架中巧用樹選擇器的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12