javascript - antdesign底层弹出个confirmModal。怎么获取底层的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() { },}); },
这个this我怎么获取不到呢,都加了bind了报错:
PaymentCollection.jsx:329 Uncaught TypeError: Cannot read property ’state’ of undefined
问题解答
回答1:你ajax的success和error都没有bind。注意看报错信息的位置。
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() {}, });},
准确来说是onOk函数的this环境已经丢失了。;
回答2:谢邀。
-----分割线----
在showConfirm外面定义个一个_this,然后用_this替换this即可。
或者你在外面let bindsuc = function(data){}.bind(this);//然后里面...success:bindsuc,//error同理...
相关文章:
1. java - 创建maven项目失败了 求解决方法2. node.js - 函数getByName()中如何使得co执行完后才return3. 一个走错路的23岁傻小子的提问4. python - 如何使用pykafka consumer进行数据处理并保存?5. javascript - SuperSlide.js火狐不兼容怎么回事呢6. 运行python程序时出现“应用程序发生异常”的内存错误?7. 主从备份 - 跪求mysql 高可用主从方案8. javascript - git clone 下来的项目 想在本地运行 npm run install 报错9. mysql主从 - 请教下mysql 主动-被动模式的双主配置 和 主从配置在应用上有什么区别?10. python - django 里自定义的 login 方法,如何使用 login_required()
