Passing Current Object Name to Lightning Web Component

Lets us discuss here how to pass the current object name to the lightning web component. If a component is used on a Lightning record page, you can pass the component 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: Account. 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.

 Create a lightning component

Create a new lightning web component using the below SFDX command.

Here is the ViewObject.html markup code

Here is the JavaScript controller class code .In the component’s JavaScript class, use the @api decorator to create a public objectApiName property and recordId property.

Here is the ViewObject.js-meta.xml code

Push the changes

Now push the changes to scratch org using the below SFDX command

Add this component to record page and you can able to see the output as shown below