Newer Version Available

This content describes an older version of this product. View Latest

Use Lightning Components in Visualforce Pages

Add Aura components to your Visualforce pages to combine features you’ve built using both solutions. Implement new functionality using Aura components and then use it with existing Visualforce pages.

Lightning Components for Visualforce is based on Lightning Out, a powerful and flexible feature that lets you embed Aura components into almost any web page. When used with Visualforce, some of the details become simpler. For example, you don’t need to deal with authentication, and you don’t need to configure a Connected App.

In other ways using Lightning Components for Visualforce is just like using Lightning Out. Refer to the Lightning Out section of this guide for additional details.

Important

There are three steps to add Aura components to a Visualforce page.
  1. Add the Lightning Components for Visualforce JavaScript library to your Visualforce page using the <apex:includeLightning/> component.
  2. Create and reference a Lightning app that declares your component dependencies.
  3. 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 Dependency App

To use Lightning Components for Visualforce, define component dependencies by referencing a Lightning dependency app. This app is globally accessible and extends ltng:outApp. The app declares dependencies on any Lightning definitions (like components) that it uses.

Here’s an example of a Lightning app named lcvfTest.app. The app uses the <aura:dependency> tag to indicate that it uses the standard Lightning component, lightning:button.
1<aura:application access="GLOBAL" extends="ltng:outApp"> 
2    <aura:dependency resource="lightning:button"/>
3</aura:application>

Extending from ltng:outApp adds SLDS resources to the page to allow your Lightning components to be styled with the Salesforce Lightning Design System (SLDS). If you don’t want SLDS resources added to the page, extend from ltng:outAppUnstyled instead.

Note

To reference this app on your page, use the following JavaScript code, where theNamespace is the namespace prefix for the app. That is, either your org’s namespace, or the namespace of the managed package that provides the app.
1$Lightning.use("theNamespace:lcvfTest", function() {});
If the app is defined in your org (that is, not in a managed package), you can use the default “c” namespace instead, as shown in the next example. If your org doesn’t have a namespace defined, you must use the default namespace.

For further details about creating a Lightning dependency app, see Lightning Out Dependencies.

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 includes an additional parameter, domLocator, which specifies the DOM element where you want the component inserted.

Let’s look at a sample Visualforce page that creates a lightning:button using the lcvfTest.app from the previous example.
1<apex:page>
2    <apex:includeLightning />
3
4    <div id="lightning" />
5
6    <script>
7        $Lightning.use("c:lcvfTest", function() {
8          $Lightning.createComponent("lightning:button",
9              { label : "Press Me!" },
10              "lightning",
11              function(cmp) {
12                console.log("button was created");
13                // do some stuff
14              }
15          );
16        });
17    </script>
18</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.

You can call $Lightning.use() multiple times on a page, but all calls must reference the same Lightning dependency app.

Important

For further details about using $Lightning.use() and $Lightning.createComponent(), see Lightning Out Markup.