Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Use Lightning Components in Visualforce Pages
- Add the Lightning Components for Visualforce JavaScript library to your Visualforce page using the <apex:includeLightning/> component.
- Create and reference a Lightning Out app that declares your component dependencies.
- Write a JavaScript function that creates the component on the page using $Lightning.createComponent().
Add the Lightning Components for Visualforce JavaScript Library
Add <apex:includeLightning/> at the beginning of your page. This component loads the JavaScript file used by Lightning Components for Visualforce.
Create and Reference a Lightning Out App
To use Lightning Components for Visualforce, define component dependencies by referencing a Lightning Out app. This app is globally accessible and extends ltng:outApp. The app declares dependencies on any Lightning component that it uses.
1<aura:application access="GLOBAL" extends="ltng:outApp">
2 <aura:dependency resource="lightning:button"/>
3</aura:application>1$Lightning.use("theNamespace:lcvfTest", function() {});For details about creating a Lightning Out app, see Lightning Out Dependencies in the Lightning Web Components Developer Guide.
Creating a Component on a Page
Finally, add your top-level component to a page using $Lightning.createComponent(String type, Object attributes, String domLocator, function callback). This function is similar to $A.createComponent(), but it includes an additional parameter, domLocator, that specifies the DOM element where you want the component inserted.
1<apex:page>
2 <apex:includeLightning />
3 <div id="lightning" />
4 <script>
5 $Lightning.use("c:lcvfTest", function() {
6 $Lightning.createComponent("lightning:button",
7 { label : "Press Me!" },
8 "lightning",
9 function(cmp) {
10 console.log("button was created");
11 // do some stuff
12 }
13 );
14 });
15 </script>
16</apex:page>The $Lightning.createComponent() call creates a button with a “Press Me!” label. The button is inserted in a DOM element with the ID “lightning”. After the button is added and active on the page, the callback function is invoked and executes a console.log() statement. The callback receives the component created as its only argument. In this simple example, the button isn't configured to do anything.
For details about using $Lightning.use() and $Lightning.createComponent(), see Lightning Out Markup in the Lightning Web Components Developer Guide.
Limitations
If a Visualforce page contains an Aura component, you can’t render the Visualforce page as a PDF.
Browser Third-Party Cookies
Lightning components set cookies in a user’s browser. Because Lightning components and Visualforce are served from different domains, these cookies are “third-party” cookies.
You can use several approaches for enabling Lightning components in Visualforce to work with third-party cookies. See Enable Browser Third-Party Cookies for Lightning Out in the Lightning Web Components Developer Guide.