How to get the recordId in lightning web component ?

by Rijwan Mohmmed
0 comment

Hello friends, today we will learn how to get the recordId in the lightning web component (LWC). We can get directly id by @api decorator if lightning web component on the record page.

If a component with a recordId property is used on a Lightning record page, the page sets the property to the ID of the current record.

import { LightningElement, api } from 'lwc';
export default class TechdicerClass extends LightningElement {
    @api recordId;
}

If you want to get the recordId in communities, we need to specify a parameter in targets in meta file of the component.

<targetConfigs>
    <targetConfig targets="lightningCommunity__Default">
        <property
            name="recordId"
            type="String"
            label="Record Id"
            description="Automatically bind the page's record id to the component variable"
            default="{!recordId}" />
    </targetConfig>
</targetConfigs>

When your component is invoked in a record context in a community, the expression "{!recordId}" is set to the the 18-character ID of the record.

<template>
   recordId ==>  {!recordId}
</template>

You may also like

Leave a Comment