Creating Salesforce apps with Lightning Web Components (LWC) involves several steps, from setting up your development environment to deploying your app. Here's a concise guide to help you get started.
Step 1: Set Up Your Development Environment
- Install Salesforce CLI:
- Download and install the Salesforce CLI from the Salesforce website.
- Verify the installation by running
sfdx --version
in your terminal.
- Set Up Visual Studio Code (VS Code):
- Install VS Code, a popular code editor.
- Add the Salesforce Extension Pack from the VS Code marketplace to enhance your development experience.
- Create a Salesforce DX Project:
- Open a terminal in VS Code and run
sfdx force:project:create -n myLWCPProject
to create a new project.
- Open a terminal in VS Code and run
Step 2: Authorize a Developer Hub
- Authorize Your Dev Hub:
- Run
sfdx force:auth:web:login -d -a DevHub
to authorize your Developer Hub. This step allows you to create and manage scratch orgs.
- Run
Step 3: Create and Configure a Scratch Org
- Create a Scratch Org:
- Run
sfdx force:org:create -s -f config/project-scratch-def.json -a myScratchOrg
to create a scratch org for development. - Open the scratch org with
sfdx force:org:open
.
- Run
Step 4: Create a Lightning Web Component
- Generate the Component:
- In VS Code, open the command palette (Ctrl+Shift+P) and run
SFDX: Create Lightning Web Component
. - Provide a name for your component, and specify the target directory (typically
force-app/main/default/lwc
).
- In VS Code, open the command palette (Ctrl+Shift+P) and run
- Component Files:
- Your new component will have three main files:
.html
: For the component's HTML template..js
: For JavaScript logic..css
: For styles (optional).
- Your new component will have three main files:
Step 5: Develop Your Component
- HTML File:
-
Define the structure of your component in the HTML file. Use standard HTML and LWC-specific directives.
<template> <h1>Hello, {name}!</h1> </template>
-
- JavaScript File:
-
Add interactivity and logic in the JavaScript file. Use ES6 modules and Salesforce’s LWC APIs.
import { LightningElement, track } from 'lwc'; export default class MyComponent extends LightningElement { @track name = 'World'; }
-
- CSS File:
- Style your component using standard CSS in the optional CSS file.
Step 6: Deploy and Test Your Component
- Deploy to Scratch Org:
- Run
sfdx force:source:push
to deploy your component to the scratch org.
- Run
- Add Component to Lightning App Builder:
- In scratch.org, navigate to the Lightning App Builder.
- Create a new app or edit an existing one and drag your component onto the page.
- Test the component.
- Save and activate the page. Preview the page to see your component in action.
Step 7: Deploy to Production
- Authorize Production Org:
- Run
sfdx force:auth:web:login -a Production
to authorize your production org.
- Run
- Deploy to Production:
- Run
sfdx force:source:deploy -u Production
to deploy your components to the production org.
- Run
Refer to learn more about Salesforce Apps with Lightning Web Components