这篇文章主要介绍了封装axios+promise通用请求函数的方法,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。
import axios from "axios"; import baseUrl from "../../setBaseUrl"; axios.defaults.baseURL = baseUrl; import { Loading, Message } from "element-ui"; const loadingOptions = { lock: true, text: "拼命加载中", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)" }; if (localStorage.token) { axios.defaults.headers.common["x-token"] = localStorage.token; } const Http = { get: function(url: string, data: any, isLoading: boolean) { if (localStorage.token) { axios.defaults.headers.common["x-token"] = localStorage.token; } let loading: any; if (isLoading) { loading = Loading.service(loadingOptions); } return new Promise<any>((resolve, reject) => { axios .get(url, { params: data }) .then(function(res) { if (isLoading) { loading.close(); } if (res.data.code === 200) { resolve(res.data.data); } else { // console.warn("Http error Info ===> ", res.data); reject(res.data.message); } }) .catch(function(err) { if (isLoading) { loading.close(); } // console.warn("Http error Info ===> ", err); Message({ message: err.message, type: "error", duration: 2500, showClose: true }); }); }).catch(function(err) { Message({ message: err, type: "error", duration: 2500, showClose: true }); }); }, post: function(url: string, data: any, isLoading: boolean) { if (localStorage.token) { axios.defaults.headers.common["x-token"] = localStorage.token; } let loading: any; if (isLoading) { loading = Loading.service(loadingOptions); } return new Promise<any>((resolve, reject) => { axios .post(url, data) .then(function(res) { if (isLoading) { loading.close(); } if (res.data.code === 200) { resolve(res.data.data); } else { reject(res.data.message); } }) .catch(function(err) { if (isLoading) { loading.close(); } // console.warn("Http error Info===>", err); Message({ message: err.message, type: "error", duration: 2500, showClose: true }); }); }).catch(function(err) { Message({ message: err, type: "error", duration: 2500, showClose: true }); }); }, put: function(url: string, data: any, isLoading: boolean) { if (localStorage.token) { axios.defaults.headers.common["x-token"] = localStorage.token; } let loading: any; if (isLoading) { loading = Loading.service(loadingOptions); } return new Promise<any>((resolve, reject) => { axios .put(url, data) .then(function(res) { if (isLoading) { loading.close(); } if (res.data.code === 200) { resolve(res.data.data); } else { reject(res.data.message); } }) .catch(function(err) { if (isLoading) { loading.close(); } // console.warn("Http error Info===>", err); Message({ message: err.message, type: "error", duration: 2500, showClose: true }); }); }).catch(function(err) { Message({ message: err, type: "error", duration: 2500, showClose: true }); }); }, delete: function(url: string, data: any, isLoading: boolean) { if (localStorage.token) { axios.defaults.headers.common["x-token"] = localStorage.token; } let loading: any; if (isLoading) { loading = Loading.service(loadingOptions); } return new Promise<any>((resolve, reject) => { axios .delete(url, data) .then(function(res) { if (isLoading) { loading.close(); } if (res.data.code === 200) { resolve(res.data.data); } else { reject(res.data.message); } }) .catch(function(err) { if (isLoading) { loading.close(); } // console.warn("Http error Info===>", err); Message({ message: err.message, type: "error", duration: 2500, showClose: true }); }); }).catch(function(err) { Message({ message: err, type: "error", duration: 2500, showClose: true }); }); } }; export default Http;
补充知识:vuex刷新页面是store被更新,如何解决数据备份?
我用的方法很简单,在App.vue中的生命周期函数created中,监听页面刷新,在页面刷新的时候,将store的数据及时存储在sessionStorage中,然后刷新完毕后就可以将sessionStorage中的数据再取出来用
created () { //页面刷新store数据备份 if (localStorage.cmstoken) { this.$router.replace("/data"); }else { this.$router.replace("/"); } if (sessionStorage.getItem("store") ) { //在页面加载时读取sessionStorage里的状态信息 this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(sessionStorage.getItem("store")))) } window.addEventListener("beforeunload",()=>{ //在页面刷新时将vuex里的信息保存到sessionStorage里 sessionStorage.setItem("store",JSON.stringify(this.$store.state)) }) },
感谢你能够认真阅读完这篇文章,希望小编分享封装axios+promise通用请求函数的方法内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。