javascript - antdesign底層彈出個(gè)confirmModal。怎么獲取底層的this?
問題描述
showConfirm() {//彈出確認(rèn)對(duì)話框confirm({ title: ’當(dāng)前總計(jì)應(yīng)收金額為’+this.state.allReceivablePrice+’元’,//這里能得到值!!!! // content: ’some descriptions’, okText: ’確認(rèn)回款’, 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() { },}); },
這個(gè)this我怎么獲取不到呢,都加了bind了報(bào)錯(cuò):
PaymentCollection.jsx:329 Uncaught TypeError: Cannot read property ’state’ of undefined
問題解答
回答1:你ajax的success和error都沒有bind。注意看報(bào)錯(cuò)信息的位置。
showConfirm() {//彈出確認(rèn)對(duì)話框 confirm({title: ’當(dāng)前總計(jì)應(yīng)收金額為’+this.state.allReceivablePrice+’元’,//這里能得到值!!!!// content: ’some descriptions’,okText: ’確認(rèn)回款’,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() {}, });},
準(zhǔn)確來說是onOk函數(shù)的this環(huán)境已經(jīng)丟失了。;
回答2:謝邀。
-----分割線----
在showConfirm外面定義個(gè)一個(gè)_this,然后用_this替換this即可。
或者你在外面let bindsuc = function(data){}.bind(this);//然后里面...success:bindsuc,//error同理...
相關(guān)文章:
1. php多任務(wù)倒計(jì)時(shí)求助2. 為何 localStorage、sessionStorage 屬于html5的范疇,但是為何 IE8卻支持?3. win10 python3.5 matplotlib使用報(bào)錯(cuò)4. MySQL的聯(lián)合查詢[union]有什么實(shí)際的用處5. mysql 遠(yuǎn)程連接出錯(cuò)10060,我已經(jīng)設(shè)置了任意主機(jī)了。。。6. html5 - css3scale和rotate同時(shí)使用轉(zhuǎn)換成matrix寫法該如何轉(zhuǎn)換?7. 默認(rèn)輸出類型為json,如何輸出html8. PHP訂單派單系統(tǒng)9. python的正則怎么同時(shí)匹配兩個(gè)不同結(jié)果?10. 數(shù)組排序,并把排序后的值存入到新數(shù)組中
