search

Home  >  Q&A  >  body text

javascript - A confirmModal pops up at the bottom of antdesign. How to get the underlying this?

showConfirm() {//弹出确认对话框
        confirm({
            title: '当前总计应收金额为'+this.state.allReceivablePrice+'元',//这里能得到值!!!!
            // content: 'some descriptions',
            okText: '确认回款',
            cancelText: '取消',
            onOk() {
                const {allSelectOrder}=this.state;
                if (allSelectOrder.length==0){
                    message.error('订单Id不能为空');
                    return;
                }else {
                    this.setState({loading: true});
                    $.ajax({
                        url: API.flow,
                        type: 'post',
                        dataType: 'json',
                        data: JSON.stringify(allSelectOrder),
                        contentType: 'application/json;charset=UTF-8',
                        success: ()=> {
                            this.setState({
                                loading: false,
                            });
                            message.success('添加收款记录成功!');
                            this.refreshData();
                        },
                        error: (data)=> {
                            Modal.error({
                                title: data.responseJSON.msg
                            });
                            this.setState({ loading: false });
                        }
                    })
                }
            },
            onCancel() {

            },
        });
    },

Why can’t I get this, I have added bind
Error:

PaymentCollection.jsx:329 Uncaught TypeError: Cannot read property 'state' of undefined
某草草某草草2811 days ago541

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你2017-05-16 13:38:54

    Your ajax success and error are not bound. Pay attention to the location of the error message.

    showConfirm() {//弹出确认对话框
        confirm({
            title: '当前总计应收金额为'+this.state.allReceivablePrice+'元',//这里能得到值!!!!
            // content: 'some descriptions',
            okText: '确认回款',
            cancelText: '取消',
            onOk: () => {
                const {allSelectOrder}=this.state;
                if (allSelectOrder.length==0){
                    message.error('订单Id不能为空');
                    return;
                }else {
                    this.setState({loading: true});
                    $.ajax({
                        url: API.flow,
                        type: 'post',
                        dataType: 'json',
                        data: JSON.stringify(allSelectOrder),
                        contentType: 'application/json;charset=UTF-8',
                        success: ()=> {
                            this.setState({
                                loading: false,
                            });
                            message.success('添加收款记录成功!');
                            this.refreshData();
                        },
                        error: (data)=> {
                            Modal.error({
                                title: data.responseJSON.msg
                            });
                            this.setState({ loading: false });
                        }
                    })
                }
            },
            onCancel() {
    
            },
        });
    },

    To be precise, the this environment of the onOk function has been lost. ;

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:38:54

    Thanks for the invitation.

    -----Separating line----

    Define _this outside showConfirm, and then replace this with _this.

    或者你在外面
    
    let bindsuc = function(data){}.bind(this);
    
    //然后里面
    ...
    success:bindsuc,
    //error同理
    ...

    reply
    0
  • Cancelreply