在Quick Action中引用lwc组件时,recordId与objectApiName会undefined,但html页面中用到recordId的地方能正常使用,在connectedCallback中打印recordId却找不到,因为在Quick Action中获取recordId,与connectedCallback并非顺序执行,通常在调用connectdCallback后填充。
如果想在Quick Action中根据recordId执行一下逻辑
1、可以将此逻辑放在get set中去执行,如下:
@api set recordId(value) {
this._recordId = value;
// 根据记录id去执行的逻辑
}
get recordId() {
return this._recordId;
}
2、可以使用CurrentPageReference获取当前页面的URL信息,如下:
import {CurrentPageReference} from 'lightning/navigation';
@wire(CurrentPageReference)
getStateParameters(currentPageReference) {
if (currentPageReference) {
this.recordId = currentPageReference.state.recordId;
this.currentUrl = currentPageReference.state.backgroundContext;
this.currentPageObjectName = currentPageReference.attributes.objectApiName
}
}
此外,recordPage中是可以使用recordId与objectApiName的。
附上外网对此问题的解答:https://salesforce.stackexchange.com/questions/344045/recordid-is-undefined-in-lwc-quick-action-component
https://salesforcediaries.com/2024/06/10/get-recordid-in-lwc-quick-action/