詳解Vue整合axios的實(shí)例代碼
在vue開(kāi)發(fā)中,不可避免要整合axios,簡(jiǎn)單記錄一下整合中的文件,方便以后使用查找。
整合文件axios.js
import axios from 'axios'; // 適配vue-resource const instance = axios.create(); instance.interceptors.request.use(config=> { //Serialize.decode(config); return config; }); instance.interceptors.response.use(response=> { return response.data; }, err=> { if (err.response) { axios.post('/v1/error', err.response); return Promise.reject(err.response.data); } return Promise.reject({ code: 1024, message: err.message }); }); functionplugin(Vue){ if (plugin.installed) { return; } Vue.http = instance; } if (typeof window !== 'undefined' && window.Vue) { window.Vue.use(plugin); } export default plugin;
vue插件使用 app.js
import Vue from 'vue'; import App from './App.vue'; import store from './store'; import { sync } from 'vuex-router-sync'; import router from './router'; import * as filters from './filters'; import yxui from 'yxui/dist/yxui.min'; import axios from './axios'; Vue.use(yxui); Vue.use(axios); // sync the router with the vuex store. // this registers `store.state.route` sync(store, router); // register global utility filters. Object.keys(filters).forEach(key=> { Vue.filter(key, filters[key]); }); // create the app instance. // here we inject the router and store to all child components, // making them available everywhere as `this.$router` and `this.$store`. const app = new Vue({ router, store, ...App }); // expose the app, the router and the store. // note we not mounting the app here, since bootstrapping will be // different depending on whether we are in browser or on the server. export { app, router, store };
在vuex action 中使用:
actions: { // adList [TypesAds.AD_GET_LIST](ctx, params){ return Vue.http.get('/v1/api/ads/list', {params}).then(data=> { ctx.commit(TypesAds.AD_GET_LIST, data); return data; }).catch(err=> { //統(tǒng)一錯(cuò)誤處理 Vue.$message.error(err.msg); }); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue+elemen實(shí)現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn)
el-tooltip組件本身就是懸浮提示功能,在對(duì)它進(jìn)行二次封裝時(shí),實(shí)現(xiàn)超出的文本浮現(xiàn),本文就來(lái)介紹一下vue+elemen實(shí)現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn),感興趣的可以了解一下2023-12-12Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼
本篇文章主要介紹了Vue resource中的GET與POST請(qǐng)求的實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07利用Vite2和Vue3實(shí)現(xiàn)網(wǎng)站國(guó)際化的全過(guò)程
vite2已經(jīng)出來(lái)一段時(shí)間了,最近沒(méi)忍住嘗試了一下,這篇文章主要給大家介紹了關(guān)于利用Vite2和Vue3實(shí)現(xiàn)網(wǎng)站國(guó)際化的相關(guān)資料,需要的朋友可以參考下2021-08-08詳解input組合事件如何監(jiān)聽(tīng)輸入中文
這篇文章主要為大家介紹了input組合事件如何監(jiān)聽(tīng)輸入中文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Vue通過(guò)v-for實(shí)現(xiàn)年份自動(dòng)遞增
這篇文章主要為大家詳細(xì)介紹了Vue通過(guò)v-for實(shí)現(xiàn)年份自動(dòng)遞增,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09vue打包部署到springboot并通過(guò)tomcat運(yùn)行的操作方法
這篇文章主要介紹了vue打包部署到springboot并通過(guò)tomcat運(yùn)行的操作方法,本文通過(guò)實(shí)例圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05vue里input根據(jù)value改變背景色的實(shí)例
今天小編就為大家分享一篇vue里input根據(jù)value改變背景色的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09基于vue實(shí)現(xiàn)頁(yè)面滾動(dòng)加載的示例詳解
頁(yè)面內(nèi)容太多會(huì)導(dǎo)致加載速度過(guò)慢,這時(shí)可考慮使用滾動(dòng)加載即還沒(méi)有出現(xiàn)在可視范圍內(nèi)的內(nèi)容塊先不加載,出現(xiàn)后再加載,所以本文給大家介紹了基于vue實(shí)現(xiàn)頁(yè)面滾動(dòng)加載的示例,需要的朋友可以參考下2024-01-01