Do we have alternative of force:hasRecordId for LWC

In looking at this page of the LWC developer guide, I have not found any details around this.

Clearly to get an LWC working for a record page you can use target for RecordPage as below, making this component available to Account Page. But I need the recordId as well

<targets>
    <target>lightning__AppPage</target>
    <target>lightning__RecordPage</target>
    <target>lightning__HomePage</target>
</targets>

What is required in order to make the record ID available to my component?

Answer

You can get the recordId by creating a property with @api recordId;.

Example:

// testClass.js
import { LightningElement, api } from 'lwc';
export default class TestClass extends LightningElement {
    @api recordId;
} 

Attribution
Source : Link , Question Author : Mekyush Jariwala , Answer Author : John Towers

Leave a Comment