Hey cafeForce users,
Displaying lightning component on visualforce page is very easy & a trick of 3 minutes.
- Add the ‘Lightning Components for Visualforce’ JS (JavaScript) library to your VF page using the <apex:includeLightning/> component.
- Create a lightning outApp Lightning app that declares your component dependencies.
- Write a JavaScript function on the visualforce page that creates the component on the page dynamically using Lightning App using $Lightning.createComponent().
Let’s create a simple lightning component and use it on a visualforce page.
Steps:
Step-1: Open the LightningOnVF.cmp and paste the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<aura:component> <!-- Attributes to be set from visualforce page --> <aura:attribute name="title" type="String" default="Custom Title"/> <aura:attribute name="isLightningExperience" type="Boolean" default="Custom Title"/> <!-- Lightning Card --> <lightning:card> <aura:set attribute="title"> <lightning:icon iconName="utility:connected_apps" size="small"/> {! v.title } </aura:set> <aura:set attribute="footer"> <lightning:badge label="Tag1"/> <lightning:badge label="Tag2"/> </aura:set> <p class="slds-m-around_small">IsLightningExperience : {!v.isLightningExperience}</p> <hr class="slds-m-around_xx-small" /> </lightning:card> </aura:component> |
Step-2: Open the CustomApp.app and paste the below code.
1 2 3 4 5 |
<aura:application extends="ltng:outApp" access="global"> <aura:dependency resource="c:LightningOnVF"/> </aura:application> |
So our component is now ready to use, let’s stick it to VF page now.
Step-3: Open the LightningVFpage.page and paste the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<apex:page showHeader="true" sidebar="false"> <apex:includeLightning /> <div id="lightning" /> <script> $Lightning.use("c:CustomApp", function() { $Lightning.createComponent( "c:LightningOnVF", { 'title':'Lightning On VF Page', 'isLightningExperience': isLightningExperience() },"lightning", function(cmp) { console.log( 'Lightning Component Loaded' ); }); }); function isLightningExperience() { if( '{!$Currentpage.parameters.sfdcIFrameOrigin}' != '' ) return true; return false; } </script> </apex:page> |
Our code is ready now, run it and check if your component open here.
Also, Check:
For any queries or suggestions regarding this post, comment below:
Cheers … 🙂
Thanks for posting this particular message and making it public
Welcome Coard
Thanks working
Welcome Raghu. Let me know if you stuck somewhere.