Way 1
In js-meta.xml, set targets page
<isExposed>true<isExposed>
<targets>
<target>lightning__RecordPage<target/>
</targets>
Then use @api to get recordId
import { LightningElement, api } from 'lwc';
export default class Sample extends LightningElement {
@api recordId;
}
Way 2
Use CurrentPageReference
import { LightningElement, wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
export default class Sample extends LightningElement {
currentPageReference;
@wire(CurrentPageReference)
setCurrentPageReference(currentPageReference) {
this.currentPageReference = currentPageReference;
console.log('recordId: ' + this.currentPageReference.attributes.recordId);
}
}
No comments:
Post a Comment