封装 axios+promise通用请求函数操作
程序员文章站
2022-05-11 20:17:48
我就废话不多说了,大家还是直接看代码吧~import axios from "axios";import baseurl from "../../setbaseurl";axios.defaults....
我就废话不多说了,大家还是直接看代码吧~
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通用请求函数操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: 春秋时期长勺之战的结果如何?竟间接促成了齐鲁息兵言和
下一篇: 惠普打印机怎么设置关闭SNMP选项?