Salesforce: Create your first Lightning Web Component (LWC) component with simple steps

Welcome you all to learn how to create your first LWC component. Please follow the below steps.

  1. Install VS Code.
  2. Install CLI.
  3. Install Salesforce Extension Pack (Expanded) extension in VS Code.
  4. Create a project followed by-->click on manage button or use ctl+shift+p to open command palette-->SFDX:Create Project with Manifest-->Select Standard Project template(default)-->provide project name and press enter. You will see a project got created in your left side.
  5. Authorize an org with created project followed by the steps-->click on manage button or use ctl+shift+p to open command palette-->SFDX:Authorize an Org-->Select Production if you are using developer sandbox or Select Sandbox if you are using sandbox-->You will be navigated to login window. Provide your credentials to login.
  6. Create a Lightning Web Component followed by the steps-->click on manage button or use ctl+shift+p to open command palette-->SFDX:create Lightning Web Component-->Provide the name and press enter. You will see a component created under lwc folder.
  7. By default 3 files got created with component. also we can add additional .js,css and other files.
    1. firstLWC.html
    2. firstLWC.js
    3. firstLWC.js-meta.xml
  8. Let's open the firstLWC.html and add the below code.
<template>
    <lightning-card title="HelloWorld" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
            <p>Hello, {greeting}!</p>
        </div>
    </lightning-card>
</template>   
Component start and end with <template> tag. Here {} is used to bind the variable attribute.
       
        9. Open firstLWC.js file and update the code as below.
import { LightningElementfrom 'lwc';
export default class firstLWC extends LightningElement {
    greeting = 'Hello World';
    
}       
greeting is a responsive variable where this value will reflect on html page. Earlier we were using @track annotation to make a variable as responsive. 

        10. Open firstLWC.js-meta.xml and update the below code. This is used to configure the                                 component.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="firstLWC">
  <apiVersion>51.0</apiVersion>
<!-- Make is true so that this component will be available to use-->
  <isExposed>true</isExposed>
  <targets>
    <target>lightning__RecordPage</target>
    <target>lightning__HomePage</target>
  </targets>
</LightningComponentBundle>

Make sure you have added targets to your LWC else it won't available to use in flexi page. lightning_RecordPage and lightning_HomePage is used to expose the component to record page and home page respectively.
        11. Deploy the firstLWC component to the org using the below screens.
  

        11. Let's open your salesforce developer org and open home tab similar as below.
       

12. Edit home tab using edit page option.


            13. Drag your firstLWC component into the page and save it.

            14. Go to the home tab and see your component on UI.

            


Hope you have created the first LWC component successfully.


Happy Learning!!

Comments