I have a request to populate some field on component which resides on almost every page in Lightning community. The field needs to be populated with page name it resides on dynamically. Currently only solution i can think off is to get window location URL and split the string to get page name. Is there better approach for this?
1 Answer
You can make use of the Renderer element in the aura component bundle that outputs the page name when the Web Page is ready as follow -
afterRender : function(cmp, helper){
this.superAfterRender();
setTimeout(function(){
console.log(window.document.title);
cmp.set("v.pageName",window.document.title);
},0);
}
This would give you the page name when the document is ready
Now, you would want the page name to change dynamically as you navigate in your community. For that you can do the following,
Create an aura handler that looks for url change as follows -
<aura:handler event="forceCommunity:routeChange" action="{!c.handleRouteChange}"/>
And next for the action use the js code as below -
component.set("v.pageName",window.document.title);
This would reload the page name in the pageName attribute when there is a change in URL, thereby making it dynamic.