Skip to main content

LWC Development Environment

Requirement :



Setup LWC Development Environment 

1. VS (Visual Stdio) Code

2. Salesforce CLI

  • Download and install the latest version of Visual Studio Code.
  • Open the Visual Studio Code and add extensions by clicking on the extensions icon present on the left side of the VS code editor.
  • Search for Salesforce Extension Pack and install the extension. After that install Lightning Web Components extension.
  • Once you done with installing the above two extensions Re-launch the VS code editor.
  • Now it’s time to check that our environment is ready for creating Lightning Web Components by pressing Command + Shift + P on macOS or Ctrl + Shift + P on Windows and type sfdx.
LWC -step 5
Hola..!! you are now ready to create your Lightning Web Components.

Creating SFDX Project

  • First create a folder somewhere on your machine and name it trailblazer or anything of your choice.
  • Navigate to that folder from VS code editor by
    File > Open > trailblazer folder > Open
Creating SFDX Project -6
  • Now we are in the trailblazer folder, Open the terminal in the VS code editor by Terminal > New Terminal
Creating SFDX Project step-7
  • Now will create a project in the trailblazer folder by clicking Command + Shift + P on a Mac or Ctrl + Shift + P on Windows it will open the command pallete
  • Type sfdx and select SFDX:Create Project
LWC Creating SFDX Project step-8
  • Enter the project name as HelloWorldLwc and press enter.
  • After the above step a new project is created with the name HelloWorldLwc in the VS code editor with the default project structure as shown :
LWC Creating SFDX Project step-10
  • You can create the project using the command in the terminal of the VS Code.
  • Now cd HelloWorldLwc to move to the project folder from the terminal

Authorize your Developer Edition.

  • Open the Command Palette in the VS code by using the Command + Shift + P on a Mac or Ctrl + Shift + P on Windows
    View > Command Palette
  • Select SFDX: Authorize an Org
LWC Authorize your Developer Edition1
  • Select Project Default
LWC Authorize your Developer Edition2
  • Give the alias name(myWorldOrg) or leave blank
  • After the alias name you will be taken into the login screen where you have give your login credentials of salesforce org.
LWC Authorize your Developer Edition4
  • You can authorize the same by using the CLI with the following command in terminal
  • After running the above command it will redirect to the browser asking for the credentials for authorization . Give the username and password of the org u have signed up.
  • You will see a message as successfully authorized in the terminal after entering your credentials in the browser.
LWC Authorize your Developer Edition5
To check the org’s that are connected or authorized by using this command

–all : 
it will give all the orgs that are active and inactive
LWC Authorize your Developer Edition6

Creation of Lightning Web Component :

  • In the VS code , Press Command + Shift + P on a Mac or Ctrl + Shift + P on Windows.
  • Type sfdx then search for sfdx: create lightning web component and select.
Creation of Lightning Web Component step18
  • Select the path in which the Lightning Web Component to be created. By default the path is under force-app/main/default/lwc.
Creation of Lightning Web Component step19
  • After that enter the name of the Lightning Web Component as helloExpression (anything of your choice) and press enter.
  • Now you have successfully created your first LIghtning Web Component helloExpression.
  • You can check your component under the lwc folder in VS Code editor
  • Another way of creating the Lightning Web Component is by salesforce cli .
  • Use this command in the terminal to create a Lightning Web Component and make sure that you are in the project folder in the terminal.

–type : 
specifies which type of component you are creating i.e, –type lwc for lightning web component   and –type aura for aura components.
-n : specifies the name of the component .

-d : specifies the directory in which we are creating the component.
  • You will see the above screen when the lightning web component is created.
Code for the helloExpression LWC component.

helloExpression.html

<template>
    <lightning-card title="HelloWorld" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
            <p>Hello, {greeting}!</p>
            <lightning-input 
                label="Name" 
                value={greeting} 
                onchange={changeHandler}>
            </lightning-input>
        </div>
    </lightning-card>
</template>

helloExpression.js
import { LightningElementtrack } from 'lwc';
export default class HelloWorld extends LightningElement {
    @track greeting = 'World';
    changeHandler(event) {
        this.greeting = event.target.value;
    }
}
helloExpression.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>48.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
        <target>lightning__Tab</target>
      </targets>
</LightningComponentBundle>

Deploying the component to Org : 

  • In the visual studio code, select the default folder in the project.
  • Right-click on the default folder, there you will find an option SFDX:Deploy Source to org and select that.
Deploying the component to Org step 23
  • After the successful deployment you will see the following message in the terminal
Deploying the component to Org step 24
  • You can do the same deployment by using the terminal with the command mentioned below

-p : 
specifies the path from which directory you want to deploy to org from VS Code
u : specifies the username of the org to which you are deploying the component
Deploying the component to Org step 25
  • Now to open the Org to which we have deployed the component use the command
It will automatically open the org in the browser.

Finding the Lightning Web Component in the org:

  • In the org select the sales app.
  • Edit the home page of the sales app by clicking the gear icon at the top right corner and then Edit
Finding the Lightning Web Component in the org step26

  • After selecting the Edit Page you will be navigated to Lightning App Builder of the home page. It should look something like the below page.
  • Now on the left hand side search for the component  helloExpression we have created
Finding the Lightning Web Component in the org step 28
  • Drag the component on to screen and place it somewhere on the right top corner
  • Then Click
    Save > Activate > Assign as Org Default
Open the sales app from the app launcher .will see the our component at the corner.
Finding the Lightning Web Component in the org step 30

Other Useful Blog

Lightning Component

Happy Learning !!


Comments

Post a Comment

Popular posts from this blog

LWC Decorators

Decorators : ‹ › In LWC decorators are used to add functionality to a property or function. There are three types of decorators : 1. @track 2. @api 3. @wire @track : It is used to make properties or methods in LWC is private. This properties is not accessible outside the LWC in which it is declared. @api : It is used to make properties or methods in LWC is public. This way they are accessible outside of the component in which they are declared. For ex : parent component can set the value of the properties in the child component. @wire : It is used to read the Salesforce data from apex class into LWC. It is generally used while making the server call to apex class. LWC Properties : There are two type of LWC properties. 1. Reactive Properties 2. Non-Reactive Properties Reactive Properties : In case of reactive properties , value of the properties change when the component re-render. There are two types of Reactive Properties. 1. Public properties  -

LWC Show Records In Table

Requirement : ‹ › Create a LWC component which will show the most recently created 10 Opportunity from the org and show them in table having Name , Close Date , Stage Name , Type and Create . or in other words Show Opportunity data in table format. cmpOpportunityRecords.html < template >      < lightning-card   title = "Opportunity Records"   icon-name = "standard:opportunity" >                  < lightning-datatable                            data = {data}                columns = {columns}                key-field = "Id" >          </ lightning-datatable >      </ lightning-card > </ template > cmpOpportunityRecords.js import  {  LightningElement  , api , wire , track  }  from   'lwc' ; import   getOpportunity   from   '@salesforce/apex/cmpOpportunityRecordsCls.getOpportunity' ; export   default   class   DevCmpShowRecords   extends   LightningElement  {     @ track   columns  =

LWC Life Cycle

Requirement : ‹ › LWC Life Cycle : Life Cycle is nothing but a callback method which is triggered at a specific phase of a component's lifecycle. LWC lifecycle contains the following phases: 1. constructor() 2. connectedCallback() 3. disconnectedCallback() 4. render() 5.renderedCallback() 6. errorCallback(error,stack) 1. constructor(): constructor is invoked when the component is created, it is similar to Init method in aura component. Only difference here is that, flow is from Parent to Child, and opposite to that of Init where child component Init is called first then the parent component Init gets called. First Statement must be super() with no parameters. It is required to assign the proper prototype and value to this attribute. 2. connectedCallback(): connectedCallback() is invoked when the component is inserted into DOM. 3. disconnectedCallback(): disconnectedCallback() is invoked when the component is removed from DOM. 4. render(): render(