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. docker網絡端口映射,沒有方便點的操作方法么?2. java - spring boot 如何打包成asp.net core 那種獨立應用?3. javascript - 關于apply()與call()的問題4. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?5. java - 在用戶不登錄的情況下,用戶如何添加保存到購物車?6. datetime - Python如何獲取當前時間7. javascript - nginx反向代理靜態資源403錯誤?8. docker start -a dockername 老是卡住,什么情況?9. 安全性測試 - nodejs中如何防mySQL注入10. python - 調用api輸出頁面,會有標簽出現,請問如何清掉?
