Friday, 2 August 2024

NavigationMixin module in LWC

 We know that the NavigationMixin module is used for navigating between different pages or components within the Salesforce Lightning Experience, Salesforce Mobile App, and Lightning communities from an LWC.


The NavigationMixin adds two APIs to your component's class.


1) [NavigationMixin.Navigate](pageReference, [replace]) - A component calls this API to navigate to another page in the application.


    ex :

      this[NavigationMixin.Navigate]({

            type: 'standard__recordPage',

            attributes: {

                recordId: this.yourRecordId,

                objectApiName: 'Account',

                actionName: 'view'

            },

        });

2) [NavigationMixin.GenerateUrl](pageReference) - A component calls this API to get a promise that resolves to the resulting URL. The component can use the URL in the href attribute of an anchor. It can also use the URL to open a new window using the window.open(url) browser API.


Example of opening up a window in a new Tab


 // Generate a URL to a User record page

 this[NavigationMixin.GenerateUrl]({

   type: 'standard__recordPage',

   attributes: {

     recordId: this.yourRecordId,

     actionName: 'view',

  },

 }).then((url) => {

  // Opens the tab in new page

  window.open(url, "_blank");

});

No comments:

Post a Comment