如何使用refreshApex在Wire中刷新datatable


@wire(_getContacts,{recordId:'$recordId'}) wiredContacts({error,data}){
this.dataToRefresh = data;
if (data) {
this.contacts = this.dataToRefresh.recordList;
this.ContactsRecords = this.dataToRefresh.cList;           
this.contactsSize = " Case Contacts (" + this.contacts.length + ")";           
}else{
//
}
};
relateContacts() {
this.showSpinner = true;
this.showtable=false;
relateContacts({contacts: this.selected, recordId: this.recordId})
.then(data => {
this.showSpinner=false;
this.showtable=true;
this.showSuccessMessage(); 

refreshApex(this.dataToRefresh);               

//location.reload();
this.isShowModal = false; 
})
.catch(error => {
console.log(error);
this.showSpinner=false;
const evt = new ShowToastEvent({
title: 'Application Error',
message: error.body.message,
variant: 'error',
mode: 'sticky'
});
this.dispatchEvent(evt);
this.showSpinner = false;
});
}

对于这段代码,我尝试了所有可能的方法。但我不确定这里的小姐。我看了所有的博客,但每个地方都提到了解决方案。

尝试如下:

@wire(_getContacts,{recordId:'$recordId'}) wiredContacts({data}){
this.dataToRefresh = data;
But this also does not work

啊,这是一个有趣的!您的问题是在wiredContacts中使用解构作为参数。({data}或{data,error}通常作为回调函数的参数,除非您必须执行refresh)
试试这个

@wire(_getContacts,{recordId:'$recordId'}) wiredContacts(value){
this.dataToRefresh = value;
const {data, error} = value;
//Rest of you code now with data and error 

}

然后在你的另一个方法中你可以这样做:

method(){    
refreshApex(this.dataToRefresh);
}

Salesforce确实在他们的示例代码中展示了如何做到这一点,但很容易忽略它,并体验到您从中获得的乐趣。
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_result_caching

参见他们页面上的最后一个例子。

最新更新