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. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?2. css3 - 微信前端頁面遇到的transition過渡動畫的bug3. javascript - 請教如何獲取百度貼吧新增的兩個加密參數4. python - 編碼問題求助5. python - 關于計算日期的問題6. (python)關于如何做到按win+R再輸入文件文件名就可以運行?7. Python如何播放還存在StringIO中的MP3?8. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處9. mysql 一個sql 返回多個總數10. mysql - 如何減少使用或者不用LEFT JOIN查詢?
