Navigate from one LWC component to another LWC component with/without Aura
on
Get link
Facebook
X
Pinterest
Email
Other Apps
To navigate in Lightning Experience, Experience Builder sites, or the
Salesforce mobile app, we define a
PageReference
object. The
PageReference
type generates a unique URL format and defines attributes that apply to all
pages of that type. Lightning web components allows us to navigate from one
LWC component to another LWC by using these different
page reference
types and they are equally supported in lightning experience and salesforce
mobile app.
Now consider you have one LWC component, hereafter we will name it as
sourceLWC, and another LWC component which will be called as
targetLWC. Now if you want to navigate from sourceLWC to
targetLWC, we have couple of approaches.
Navigation using AURA: To make a lightning component
url Addressable, we have to implement isUrlAddressable
interface in the component. This is only available in AURA and currently no
support exists in LWC. So bind your targetLWC component in the
targetLWCAura which implements isUrlAddressable interface.
Navigation without using AURA: We all are aware that any url that is
rendered fully, becomes a standard web page. So using the standard__webPage
of many available PageReference types in lightning navigation in
conjunction with /one/one.app#hash url, we can make the LWC
addressable. In order to make our LWC fully addressable, we also have to
encode our component definition to base64 before passing it to url
navigation.
Having gone through with the above design and approaches, let's make it
work. Do create LWC components and AURA component as outlined below:
sourceLWC
targetLWC
targetLWCAura
As we have understood enough on the implementation approach and code is
self explanatory so we will jump directly on code part.
sourceLWC.html
<template>
<divclass="slds-card">
<divclass="slds-box">
<lightning-buttonlabel="Navigate To Another LWC Without Using Aura"
title="Navigate To Another LWC Without Using Aura"onclick={navitageToLWCWithoutAura}variant="success">
</lightning-button>
</div>
<divclass="slds-box">
<lightning-buttonlabel="Navigate To Another LWC Using Aura"
title="Navigate To Another LWC Without Using Aura"onclick={navitageToLWCUsingAura}variant="brand">
<c:targetLWClabel="Navigated From Another LWC Using Aura"/>
</aura:component>
Demo Time:
Now as you have developed and deployed your code base to your org, embed your
sourceLWC component on the home page of your app to test our
implementation.
Note: Notice the url formed with our these examples of navigation,
thank you for the step by step guide.. i tried it with Aura component and it worked for me. I have been trying to understand this from a long time and was not able to but your steps are very clear. i ahve 1 more requirement where i have an aura application for both my LWC how to navigate between them?
we tried with BTOA but when navigating one lwc to another lwc its opening in separate tab with tab leaving current tab as it is..so please be cautious opting this option..
Thank you for helping.It worked!!!
ReplyDeletethank you for the step by step guide.. i tried it with Aura component and it worked for me. I have been trying to understand this from a long time and was not able to but your steps are very clear. i ahve 1 more requirement where i have an aura application for both my LWC how to navigate between them?
ReplyDeleteBut it's not working service console with BTOA as it is opening new tab sometimes
ReplyDeletewe tried with BTOA but when navigating one lwc to another lwc its opening in separate tab with tab leaving current tab as it is..so please be cautious opting this option..
ReplyDeleteit was really helpful
ReplyDelete