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

首頁 > 語言 > JavaScript > 正文

詳解Vue整合axios的實例代碼

2024-05-06 15:15:37
字體:
來源:轉載
供稿:網友

在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 });});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=> {//統一錯誤處理Vue.$message.error(err.msg);});}}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 湖南省| 徐水县| 邹平县| 石河子市| 乌拉特后旗| 雅江县| 偃师市| 肇源县| 涟源市| 和静县| 阿合奇县| 克东县| 定陶县| 和平县| 新建县| 石城县| 永安市| 长治县| 镇安县| 罗城| 易门县| 建宁县| 西充县| 双鸭山市| 孟村| 泰安市| 高陵县| 乌拉特后旗| 屏东市| 新沂市| 武汉市| 祥云县| 岳普湖县| 新民市| 建阳市| 海安县| 天全县| 上杭县| 黄陵县| 榆树市| 岢岚县|