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. boot2docker無法啟動2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker-compose中volumes的問題4. nignx - docker內nginx 80端口被占用5. docker安裝后出現Cannot connect to the Docker daemon.6. java - SSH框架中寫分頁時service層中不能注入分頁類7. dockerfile - 為什么docker容器啟動不了?8. node.js - antdesign怎么集合react-redux對input控件進行初始化賦值9. docker容器呢SSH為什么連不通呢?10. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?

網公網安備