国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue開發中整合axios的文件整理

2019-11-19 16:41:20
字體:
來源:轉載
供稿:網友

前言

大家在vue日常開發中,不可避免要整合axios,這里簡單記錄一下整合中的文件,方便以后使用查找。下面來看看詳細的介紹。

整合方法

整合文件axios.js

import axios from 'axios';// 適配vue-resourceconst 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 });});function plugin(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=> {//統一錯誤處理Vue.$message.error(err.msg);});}}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 门源| 洛南县| 蚌埠市| 永德县| 锡林郭勒盟| 花莲县| 敖汉旗| 灵寿县| 视频| 新田县| 贵溪市| 长宁县| 和顺县| 九龙县| 新晃| 玉门市| 肇东市| 天长市| 上林县| 库车县| 岐山县| 郴州市| 彭泽县| 沈阳市| 阜平县| 濮阳市| 大宁县| 舒兰市| 新乐市| 榕江县| 柳林县| 南投市| 晴隆县| 陕西省| 班玛县| 安义县| 永宁县| 阳泉市| 东阿县| 石台县| 东乡|