Antd Modal框的this指向处理

技术栈:react、antd3、typescript

Modal对话框

在使用antd的Modal时,Modal使用confirm() 方法可以快捷地弹出确认框:

function showConfirm() {
    confirm({
        title: 'Do you Want to delete these items?',
        content: 'Some descriptions',
        onOk() {
            console.log('OK');
        },
        onCancel() {
            console.log('Cancel');
        },
    });
}

在使用过程中,需要在onOk()中做一些事情,比如要用axios进行网络请求:

function showConfirm() {
    confirm({
        title: 'Do you Want to delete these items?',
        content: 'Some descriptions',
        onOk() {
           // console.log('OK');
           this.axiosGetUserList();
        },
        onCancel() {
            console.log('Cancel');
        },
    });
}

这时候的这个this指向undefined,我们应该改变this的指向,指向该组件,所以我们可以这样改造一下:

function showConfirm() {
    confirm({
        title: 'Do you Want to delete these items?',
        content: 'Some descriptions',
        onOk: () => {
           // console.log('OK');
           this.axiosGetUserList();
        },
        onCancel() {
            console.log('Cancel');
        },
    });
}

参考链接: https://blog.csdn.net/Luckyzhoufangbing/article/details/88187381

你可能感兴趣的:(Antd Modal框的this指向处理)