How to create a Community Lightning Component

Bynayakdillip

How to create a Community Lightning Component

Building Custom Lightning Components for salesforce Communities

Step:-1 Create Apex Class HelloWorld.apxc

public class HelloWorld {
@AuraEnabled
    public static string getUserName()
    {
        return userinfo.getName();
    }
}

Step:-2 Create Lightning Component with name UserInfoComponent.cmp

<aura:component controller="HelloWorld" implements="forceCommunity:availableForAllPageTypes" access="global" >
	<aura:attribute name="userName" type="string"></aura:attribute>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <p>Hello <b>{!v.userName}</b>,How Are You</p>
</aura:component>

Step:-3 Create Component Controller for doInit Action

({
    doInit : function(component, event, helper) {
        helper.getUserRecord(component);	
    }
})

Step:-4 Create Component helper for doInit

({
    getUserRecord : function(component) {
        var action=component.get("c.getUserName");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (component.isValid() && state === "SUCCESS") {
                var userName = response.getReturnValue();
                component.set("v.userName",userName);
            }
        });
        $A.enqueueAction(action);
        
        
    }
})

Step:5 Open Community builder drag the particular component from the custom component


Step:6 Publish the site

After Publish it look like

you can visit below link how to create community site
how-to-create-community-site-using-salesforce

About the author

nayakdillip administrator

Leave a Reply