You already know that Lightning Web Components is a new programming model introduced by Salesforce for building Lightning components. In this article, we’ll discuss what tools you need to build Lightning web components and how to build one.

To create and develop Lightning Web Components and use their powerful features and performance benefits, you need to set up Salesforce DX. You also use Visual Studio Code, which is the recommended Salesforce development environment. You write a simple Lightning web component and add it to a page in Lightning Experience. Below are the steps to create a Lightning Web Component with conditional rendering.

Steps to build Lightning web components

  1. Setup Visual Studio Code
  2. Create a Project & connect to Salesforce org
  3. Create a Lightning web component
  4. Deploy & add to a Lightning page

1. Set Up Visual Studio Code

Visual Studio code editor has easy-to-install extensions for syntax highlighting, code completion, and more. First, install Salesforce extensions for Visual Studio Code. Search for Salesforce Extension Pack and click Install.

Search for Lightning Web Components and click Install. Re-launch Visual Studio Code to complete the installation.

Make sure the editor is ready by typing sfdx, you should see the commands already.

2. Create a Project & connect to Salesforce org

  1. In Visual Studio code, press Command + Shift + P on a Mac or Ctrl + Shift + P on Windows.
  2. Type SFDX.
  3. Select SFDX: Create Project
  4. Enter a Project Name & click create.
  5. Select SFDX: Authorize an Org. Login & Allow

Success message shows like this.

3. Create a Lightning web component

  1. In Visual Studio code, press Command + Shift + P on a Mac or Ctrl + Shift + P on Windows.
  2. Type SFDX.
  3. Select SFDX: Create Lightning Web Component.
  4. Press Enter to accept the default force-app/main/default/lwc.

Give a name to the component, say Cloud Fountain. This simple component displays the text ‘Hello Cloud Fountain’

This would be your HTML file. In this, we are displaying the text “hello {greeting}”. Note that greeting is a variable here

This is the .js file. In this javascript file, we are assigning a value ‘Cloud Fountain’ to the variable called ‘greeting’. @track indicates, it is a variable.

This is the .xml file. In this file, we are setting isExposed to True implies, this component is available to be used in the Lightning pages. Which type of Lightning pages is defined in the targets.

That’s it. Now you can deploy this project to Salesforce

4. Deploy & Add the component to Lightning Page

Just right click the file and select SFDX: Deploy Source to Org

After deployment, the lightning web component will be available in the App Builder as a custom component on the left side menu to use in the page.

 

Thank you for reading this post. Please feel free to leave feedback.

Related posts