美文网首页
Antd Modal框的this指向处理

Antd Modal框的this指向处理

作者: 理发师Tony | 来源:发表于2020-06-06 16:19 被阅读0次

技术栈: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指向处理

      本文链接:https://www.haomeiwen.com/subject/pgaxtktx.html