本篇参考:
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get_record_notify
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_notify_record_update
我们在Salesforce LWC学习(二十九) getRecordNotifyChange(LDS拓展增强篇)中讲述了针对LDS通知获取最新版本数据使用。在winter23的v56版本中,此方法还在正常使用,在 spring23的v57版本中,getRecordNotifyChange方法已被标记弃用,官方推荐notifyRecordUpdateAvailable方法,功能相同。
notifyRecordUpdateAvailable方法和 getRecordNotifyChange传递形参一样,针对此方法,分成两步走。
- 头部引入:
import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
- 需要刷新的地方使用:
notifyRecordUpdateAvailable(items: Array<{recordId: string}>)
需要注意的是, recordId同样需要在user interface api支持的,否则不生效。
详情:https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_all_supported_objects.htm
接下来demo进行参考。
代码语言:javascript复制import { LightningElement, wire,api,track } from 'lwc';
import { getRecord,notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import saveAccount from '@salesforce/apex/RecordNotifyChangeController.saveAccount';
import getAccount from '@salesforce/apex/RecordNotifyChangeController.getAccount';
import PHONE_FIELD from '@salesforce/schema/Account.Phone';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class RecordNotifyChangeSample extends LightningElement {
@api recordId;
@track phone;
@track industry;
@track accountName;
fields=[PHONE_FIELD,INDUSTRY_FIELD];
accountRecord;
@wire(getAccount,{recordId : '$recordId'})
wiredAccount(value) {
this.accountRecord = value;
const { data, error } = value;
if(data) {
this.industry = data.Industry;
this.phone = data.Phone;
this.accountName = data.Name;
}
}
handleChange(event) {
if(event.target.name === 'phone') {
this.phone = event.detail.value;
} else if(event.target.name === 'industry') {
this.industry = event.detail.value;
}
}
async handleSave() {
await saveAccount({ recordId: this.recordId, industry : this.industry, phone : this.phone})
.then(result => {
if(result === 'success') {
refreshApex(this.accountRecord);
notifyRecordUpdateAvailable([{recordId: this.recordId}]);
} else {
//TODO
}
})
.catch(error => {
//TODO
});
}
}
详情demo可以参考:https://boulder-bard-27f.notion.site/lightning-e757a8902c194f9bbe633b92a9d81673
总结:尽管官方弃用了此方法,但是没有提及后续会移除此方法,所以以前的方法可以保留,后续的话,推荐使用新方法。篇中有错误欢迎指出,有不懂欢迎留言。