Make a Component Aware of Its Object Context

In some use cases, it's helpful to make a component aware of its object context. For example, you want to provide the object context so that the component can return some information based on which object is being viewed on a page. To make a component aware of its object context, use the objectApiName property on a Lightning record page. The page then sets the property to the API name of the current object.

The lwc-recipes repo has a wireGetObjectInfo component that returns the object information for a selected 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.

After you include the @api decorator to create a public objectApiName property on 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