幽灵资源网 Design By www.bzswh.com
我就废话不多说了,大家还是直接看代码吧~
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通用请求函数操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...