欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Vue整合axios的實例代碼

 更新時間:2017年06月21日 15:20:06   作者:彬仔  
本篇文章主要介紹了詳解Vue整合axios的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在vue開發(fā)中,不可避免要整合axios,簡單記錄一下整合中的文件,方便以后使用查找。

整合文件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)一錯誤處理
Vue.$message.error(err.msg);
});
}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue+elemen實現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn)

    vue+elemen實現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn)

    el-tooltip組件本身就是懸浮提示功能,在對它進(jìn)行二次封裝時,實現(xiàn)超出的文本浮現(xiàn),本文就來介紹一下vue+elemen實現(xiàn)el-tooltip在文本超出區(qū)域后浮現(xiàn),感興趣的可以了解一下
    2023-12-12
  • Vue resource中的GET與POST請求的實例代碼

    Vue resource中的GET與POST請求的實例代碼

    本篇文章主要介紹了Vue resource中的GET與POST請求的實例代碼,非常具有實用價值,需要的朋友可以參考下
    2017-07-07
  • 利用Vite2和Vue3實現(xiàn)網(wǎng)站國際化的全過程

    利用Vite2和Vue3實現(xiàn)網(wǎng)站國際化的全過程

    vite2已經(jīng)出來一段時間了,最近沒忍住嘗試了一下,這篇文章主要給大家介紹了關(guān)于利用Vite2和Vue3實現(xiàn)網(wǎng)站國際化的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • 詳解input組合事件如何監(jiān)聽輸入中文

    詳解input組合事件如何監(jiān)聽輸入中文

    這篇文章主要為大家介紹了input組合事件如何監(jiān)聽輸入中文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • Vue通過v-for實現(xiàn)年份自動遞增

    Vue通過v-for實現(xiàn)年份自動遞增

    這篇文章主要為大家詳細(xì)介紹了Vue通過v-for實現(xiàn)年份自動遞增,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Vue 自定義指令功能完整實例

    Vue 自定義指令功能完整實例

    這篇文章主要介紹了Vue 自定義指令功能,結(jié)合完整實例形式分析了vue.js自定義指令相關(guān)實現(xiàn)與使用操作技巧,需要的朋友可以參考下
    2019-09-09
  • vue打包部署到springboot并通過tomcat運(yùn)行的操作方法

    vue打包部署到springboot并通過tomcat運(yùn)行的操作方法

    這篇文章主要介紹了vue打包部署到springboot并通過tomcat運(yùn)行的操作方法,本文通過實例圖文并茂的形式給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • vue使用pdf.js預(yù)覽pdf文件的方法

    vue使用pdf.js預(yù)覽pdf文件的方法

    在頁面進(jìn)行pdf預(yù)覽的時候,由于文件不能夠打印和下載很難滿足客戶的需求,接下來通過本文給大家介紹vue使用pdf.js來進(jìn)行pdf預(yù)覽,需要的朋友可以參考下
    2021-12-12
  • vue里input根據(jù)value改變背景色的實例

    vue里input根據(jù)value改變背景色的實例

    今天小編就為大家分享一篇vue里input根據(jù)value改變背景色的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 基于vue實現(xiàn)頁面滾動加載的示例詳解

    基于vue實現(xiàn)頁面滾動加載的示例詳解

    頁面內(nèi)容太多會導(dǎo)致加載速度過慢,這時可考慮使用滾動加載即還沒有出現(xiàn)在可視范圍內(nèi)的內(nèi)容塊先不加載,出現(xiàn)后再加載,所以本文給大家介紹了基于vue實現(xiàn)頁面滾動加載的示例,需要的朋友可以參考下
    2024-01-01

最新評論