Use Lightning Component into VF page – Lightning OUT

Have you ever used lightning component into your VF page? Have you ever think why we should create VF page and Lightning Component for the same functionality?

In all of my projects that I am working, I have stopped creating VF pages and Yes, it is 80% true ūüôā but I used to work on VF page for those clients that are still into Salesforce Classic.

111

I always use my lightning component into my VF page and it is right approach as everyone(Meetups, Dreamforce, Webinars, Salesforce docs) are talking about Lightning and it is future.

In this tutorial, we will create a lightning component, its controller(.jsfile), and an apex class which will display a list of Account into Lightning Component.

We will create a Lightning Application which will extend lightning out js file and a VF page where we will call that Lightning Component.

All resources used in this tutorial are listed below and can be found at my Github Repo.

1 – AccountList.cmp – The component which displays the list of All the accounts

2 – AccountListController.js – JS file the controller of AccountList component to communicate with Apex class and set the data into the component attribute.

3 – AccountController.cls – The apex class to query account records.

4 – LightningOutDemoApp.app – The app which contains the AccountList component

5 –¬†AccountListVF.vfp – The VF page, where we will call the lightning component.

Key points to remember: –

  1. use <apex:includeLightning /> tags at the beginning of VF page This component loads the JavaScript file used by Lightning Components for Visualforce.
  2. Lightning App must extend ltng:outApp library/interface.
  3. We can use $Lightning.use() many times into VF page but that must reference the same lightning app.
  4. We can only call those components that are referenced into the app.

LightningOutDemoApp.app

<aura:application access=”Global” extends=”ltng:outApp” >
                      <c:AccountList ></c:AccountList>
</aura:application>

Description of above code

access: – determines, if we can use the component throughout the Salesforce org or not acceptable values, are global, public, private

ltng:outApp: –¬†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. using¬†ltng:outApp we can access our lightning component into VF page or any external site.

AccountListVF.vfp

<apex:page >
<apex:includeLightning />

¬† ¬† ¬† ¬† <– div where we will embed our lightning component –>

<div id=”lightning” />
$Lightning.use(“c:LightningOutDemoApp”, function() {
$Lightning.createComponent(“c:AccountList”,
{
“VFpageValue” : “Woohoooo, Lightning Out is an amazing feature of Salesforce Lightning”
},
“lightning”,
function(cmp) {

});
});

</apex:page>

$Lightning.use(): – Refer the lightning application that we are using.

$Lightning.createComponent(): – Used to dynamically creating the lightning component.

You will find the complete code into my Github Repo.

Resources: –

Salesforce Document

Salesforce Document Lightning Out

Lightning Icon

Lightning Card

4 thoughts on “Use Lightning Component into VF page – Lightning OUT

  1. Nilesh Borse February 15, 2018 / 3:00 pm

    Can we include another lightning component inside VF page which is not part of the “LightningOutDemoApp” ?

    Like

    • Amit Singh February 15, 2018 / 4:31 pm

      No, We can include any other component which is not part of “LightningOutDemoApp” Application but we can include as many we want the catch for this is all components must belong to the same application.

      Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s