利用axios的cancelToken解决多个Tabs切换造成数据错乱问题

import { $post } from 'util/ajax';
import axios from 'axios';
const CancelToken = axios.CancelToken;

// post
let cancel;
export const getData = data => dispatch => {
    if (cancel !== undefined) {
        cancel();
    }
    $post({
        url: '',
        data,
        cancelToken: new CancelToken(c => { cancel = c; })
    }).then(res => {
        if (res) {
            
        } else {
            throw Error();
        }
    }).catch(error => {
        if (axios.isCancel(error)) {
        } else {
            console.log(`异常: ${error}`);
        }
    });
};


// get
let cancel;
export const getData = params => dispatch => {
    let type = '';
    if (cancel !== undefined) {
        cancel();
    }
    $get({
        url: '',
        params,
        cancelToken: new CancelToken(c => { cancel = c; })
    })
        .then(res => {
            
        })
        .catch(err => {
           
        });
};

参考链接:

  1. https://segmentfault.com/a/1190000039844280
  2. https://www.npmjs.com/package/axios

你可能感兴趣的:(前端开发,node.js,node,webpack)