Introducing Lightning Web Components
Now you can build Lightning components using two programming models: Lightning Web Components, and the original model, Aura Components. Lightning web components are custom HTML elements built using HTML and modern JavaScript. Lightning web components and Aura components can coexist and interoperate on a page. To admins and end users, they both appear as Lightning components.
Lightning Web Components uses core Web Components standards and provides only what’s necessary to perform well in browsers supported by Salesforce. Because it’s built on code that runs natively in browsers, Lightning Web Components is lightweight and delivers exceptional performance. Most of the code you write is standard JavaScript and HTML.
Salesforce is committed to developing open web standards and is a member of the World Wide Web Consortium (W3C).
Get Started Coding
The fastest way to code your first Lightning web component is in the third-party component IDE at webcomponents.dev.
Let’s create a Hello World Lightning web component and add it to the LWC starter-kit.
- In a browser, go to webcomponents.dev/create/lwc.
- Click the + sign that appears next to the src folder to create a
file, and name it helloWorld.html. Copy this code into the code
editor.
<!-- helloWorld.html --> <template> Hello, {firstName}! </template>
- Create a file and name it helloWorld.js. Copy this code into
the code
editor.
// helloWorld.js import { LightningElement } from "lwc"; export default class HelloWorld extends LightningElement { firstName = "World"; }
- To view your component, add it to the app.html file on line 5,
right before
<!-- Using an if statement -->
.
A camel case component name (helloWorld) maps to kebab case in HTML (<div class="slds-p-top_large"> <c-hello-world class="slds-text-heading_medium"></c-hello-world> </div>
hello-world
). The default namespace isc
, so the full component HTML tag is<c-hello-world>
. Also note that components must use closing tags. To reference your own components, always code with the default namespace,c
. Usec
regardless of where the code is running: in an org with or without a namespace, in a managed or unmanaged package. - To save all your changes, click the Save icon at the top right of the code editor. The Stories panel refreshes to display the Hello, World! text.
- Let’s go one step further and add a public property to the
c-hello-world
component. In helloWorld.js, importapi
, and use it to decorate thefirstName
field.// helloWorld.js import { LightningElement, api } from "lwc"; export default class HelloWorld extends LightningElement { @api firstName = "World"; }
- Because
c-hello-world
exposes a public property, a component that consumesc-hello-world
can set the property. In our example, theapp
component sets the propertyfirstName
.In
app.html
, add afirst-name
attribute to the<c-hello-world>
tag. The camel case JavaScript propertyfirstName
maps to kebab casefirst-name
in HTML.<!-- app.html --> <div class="slds-p-top_large"> <c-hello-world first-name="Lightning"></c-hello-world> </div>
- Now your component is reusable. For example, you can add multiple
c-hello-world
components to app.html and set each to a different value.<!-- app.html --> <div class="slds-p-top_large"> <c-hello-world class="slds-text-heading_medium" first-name="Web"></c-hello-world> </div> <div class="slds-p-top_large"> <c-hello-world class="slds-text-heading_medium" first-name="Components"></c-hello-world> </div>
To keep coding, jump to Data Binding in a Template. Have fun!
Other Live Code Environments
You can also play with code in these environments.
No comments:
Post a Comment