这篇文章主要介绍了封装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;
网站标题:封装axios+promise通用请求函数的方法-创新互联
当前网址:https://www.cdcxhl.com/article0/ddjiio.html
成都网站建设公司_创新互联,为您提供品牌网站建设、标签优化、商城网站、全网营销推广、静态网站、品牌网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联