I have an LWC that i placed on the contact page and would like for it to display the contacts information, but have it change to whatever contact the user is currently viewing.
so far,the card is completely blank other than the card title, but if I hard-code the contact recordId then it shows the information i want.
how can i grab the recordId?
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
const fields = [
'Contact.Name',
'Contact.Title',
'Contact.Phone',
'Contact.Email'
];
export default class WireGetRecordDynamicContact extends LightningElement {
@api recordId;
// recordId="0035e000008eB4vAAE" this is hardcoding the recordId
@wire(getRecord, { recordId: '$recordId', fields })
contact;
get name() {
return this.contact.data.fields.Name.value;
}
get title() {
return this.contact.data.fields.Title.value;
}
get phone() {
return this.contact.data.fields.Phone.value;
}
get email() {
return this.contact.data.fields.Email.value;
}
}
HTML
<template>
<lightning-card
title="WireGetRecordDynamicContact"
icon-name="standard:contact"
>
<template if:true={contact.data}>
<div class="slds-var-m-around_medium">
<p>{name}</p>
<p>{title}</p>
<p>
<lightning-formatted-phone
value={phone}
></lightning-formatted-phone>
</p>
<p>
<lightning-formatted-email
value={email}
></lightning-formatted-email>
</p>
</div>
</template>
</lightning-card>
</template>
XML
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>53.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Contact</object>
</objects>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>