0

I have a Vf page passing some data to aura component. But the below code is somehow failing to pass data from Vf to aura. Where am I going wrong?

Vf page:

<apex:page controller="DemoClass" showHeader="false" sidebar="false" action="{!fetch}">
    <script>
       window.onload=function()
       {
           var lexOrigin = "https://mydomain.lightning.force.com";                      
           var data = '{!output}';
           window.parent.postMessage(data, lexOrigin); 
       };
    </script>
</apex:page>

Apex Class

public class DemoClass
{

    @AuraEnabled public static String output {get;set;}    
    @AuraEnabled
    public static void fetch() 
    {
        output = 'Hello World';
    }   
}

Aura component:

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="DemoClass">        
    <aura:attribute name="dataFromVF" type="String" default="NOTHING"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <iframe src="/apex/Demopage"/>
    Here : {!v.dataFromVF}           
</aura:component>

JS controller:

({
    doInit: function doInit(component, event, helper) 
    {
        var vfOrigin = 'https://mydomain.visualforce.com';        
        window.addEventListener("message", $A.getCallback(function(event) {
            if (event.origin !== vfOrigin) 
            {
              // Not the expected origin: Reject the message!
              console.log('Failed'+event.data);
              return;
            }
              // Handle the message
              console.log('Print'+event.data);
              component.set('v.dataFromVF', event.data);
          }), false); 
    }
})
6
  • Have you checked this link? salesforce.stackexchange.com/questions/117438/… Mar 28, 2022 at 12:04
  • Hello, have you considered using LMS developer.salesforce.com/docs/component-library/bundle/… ?
    – Pavlo M
    Mar 28, 2022 at 12:05
  • @PavloM , I want to pass data on load of Vf page(to aura) but not on any event(such as button click etc.) from Vf. For this purpose, I have used onload in Vf page. Is the same possible using LMS? Any sample code or link to the same are really helpful. Thanks.
    – Suhasini
    Mar 28, 2022 at 12:18
  • @MuzammilBajaria, I didnt get what is explained in the link that you shared. Basically I want to pass data from vf page(on load) to the aura component and for this I need to embed the Vf in aura. If you have any sample code for this, please do share. Thanks.
    – Suhasini
    Mar 28, 2022 at 12:22
  • If you just want to pass data on load from vf to aura, you can try lightning out. You can call your lightning component from vf page. Check below link for more details. biswajeetsamal.com/blog/… Mar 28, 2022 at 12:29

0

You must log in to answer this question.

Browse other questions tagged .