zoukankan      html  css  js  c++  java
  • vue: axios

    加入Vue原型链:

    import Vue from "vue";
    import QS from "qs";
    import axios from "axios";
    import App from "./app.vue";
    import Element from "element-ui";
    import router from "./configs/routes";
    import './styles/_element.variables.scss';
    import './styles/main.scss';
    
    Vue.use(Element);
    Vue.prototype.qs = QS;
    Vue.prototype.$axios = axios;
    
    new Vue({
        router,
        render: (h) => h(App)
    }).$mount(document.getElementById('root'))

    封装axios:

    import axios from 'axios';
    
    axios.defaults.timeout = 5000;
    axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    
    axios.interceptors.request.use(
        (config) => {
            if (config.data && config.data.$skipAuthHandler) {
                config.$skipAuthHandler = true;
                delete config.data.$skipAuthHandler;
            }
            if (config.params && config.params.$skipAuthHandler) {
                config.$skipAuthHandler = true;
                delete config.params.$skipAuthHandler;
            }
            //config.headers.Authorization = _loginUser.authorization();
            return config;
        },
        (error) => {
            return Promise.reject(error)
        }
    );
    
    axios.interceptors.response.use(
        (response) => {return Promise.resolve(response);
        },
        (error) => {
            const err = error.response;
            if (err.status === 401 && !!err.config && !err.config.$skipAuthHandler) {
                //_loginUser.clear();
                window.location = '/unauthorization';
            }
            toastr.error(err.data.message);
            return Promise.reject(error);
        }
    );
    
    function fetchPost(url, params) {
        return new Promise((resolve, reject) => {
            axios.post(url, params)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                })
        })
    }
    
    function fetchGet(url, param) {
        return new Promise((resolve, reject) => {
            axios.get(url, { params: param })
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err);
                })
        })
    }
    
    function fetchRequest(param) {
        return new Promise((resolve, reject) => {
            axios.request({
                url: param.url || '',
                method: param.method || 'GET',
                data: param.datq || null,
                params: param.params || '',
                headers: {
                    'Content-Type': param.contenType || "application/json;charset=UTF-8"
                }
            }).then(res => {
                typeof resolve === 'function' && resolve(res);
            }).catch(err => {
                typeof reject === 'function' && reject(err);
            })
        })
    }
    export default { fetchGet, fetchPost, fetchRequest }

    引用

    import https from '../https.js'   // 注意用自己的路径
    
                loginPost: function () {
                    let params ={'username': 'admin', 'password': 'admin123', 'rememberMe': 'true','isMobile':'1'}
                    https.fetchPost('/login',params ).then((data) => {
                        this.base.token = data.data.token    
                       
                        this.indexPost2(this.rres)
                    }).catch(err=>{
                            console.log(err)
                        }
                    )
                },
                indexPost2:function (date) {
                    var this_ = this
                    this_.check = false
                    var jobj ={data:{'menuDate': date,'token':this.base.token}}
                    let string  = JSON.stringify(jobj)
                    let params = {dailyInfo:string}
                    https.fetchPost('/meals/mobile/getDailyMenuByDate', params)
                    .then((data) => {
                        this_.base.indexData = data
                        this_.check = true
                    })
                    .catch((err)=>{
                        console.log(err)
    
                    })
                }
            },
  • 相关阅读:
    Linux Kernel 2:用户空间的初始化
    Linux Kernel系列一:开篇和Kernel启动概要
    谢宝友:会说话的Linux内核
    如何给USB移动硬盘格式化分区
    AVR单片机最小系统 基本硬件线路与分析
    Altium Designer 基本封装
    AVR单片机命名规则
    LynxFly科研小四轴横空出世,开源,F4,WIFI --(转)
    四轴自适应控制算法的一些尝试开源我的山猫飞控和梯度在线辨识自适应等算法—(转)
    我的四轴专用PID参数整定方法及原理---超长文慎入(转)
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/10769704.html
Copyright © 2011-2022 走看看