Make a Component Aware of Its Object Context

If a component with an objectApiName property is used on a Lightning record page, the page sets the property to the API name of the current object.

In the component’s JavaScript class, use the @api decorator to create a public objectApiName property.

When your component is invoked in a record context in Lightning Experience or in the mobile app, the objectApiName is set to the API name of the object associated with the record being viewed, for example: Warehouse__c.

The objectApiName is set only when you place or invoke the component in an explicit record context. In all other cases, the objectApiName isn’t set, and your component shouldn’t depend on it.

Experience Builder sites don’t automatically bind objectApiName to a component’s template.

To achieve that goal with your component, add objectApiName in an expression in the component’s *.js-meta.xml file. All the page types specified in the targets parameter have access to the object name.

This approach works only for components where the {!objectApiName} is in the route.

When your component is invoked in an object context in an Experience Builder site, the expression objectApiName is set to the 18-character ID of the object.

See Also