Navigation in Lightning Experience Using PageReference

In this blog, I am going to explain how to Use lightning:navigation component to navigate to a given pageReference or to generate a URL from a pageReference. PageReference is a JavaScript object that represents a URL for a page. Use a PageReference instead of attempting to parse or create URLs directly. This approach helps you avoid broken navigation if Salesforce changes URL formats in the future. PageReference provides a well-defined structure that describes the page type and its corresponding attributes. A PageReference object contains key-value pairs to describe the page type you are navigating to.

The following PageReference looks like to set the route to /lightning/o/Account/list?filterName=MyAccounts

Example 

The following example generates a URL to an Account Object home based on its pageReference and sets the URL on an <a> element. The example assumes that you’re creating the component for a custom Lightning Component tab or a Lightning page by implementing  force:appHostable  and  flexipage:availableForAllPageTypes

In your client-side controller, set the pageReference attribute for the Account home page. Set the URL on your link using the generateUrl() method, which is useful for opening links in a new tab. Depending on whether you’re using the new URL format available as part of the Summer ’18 critical update, you might see a URL that begins with the new format /lightning/cmp/ or the old format one/one.app#/cmp/.

 

PageReference is a reference to a page, providing a well-defined structure that describes the page type and its corresponding values. You should use the PageReference definitions instead of attempting to parse the URL directly. The following PageReference properties are supported.

PROPERTY TYPE DESCRIPTION
type string Required. The API name of the PageDefinition.
attributes object Required. Values for each attribute specified by the PageDefinition.
state object Additional parameters, such as filterName, which is tied to the query string of the URL in Lightning Experience. The routing framework doesn’t depend on state to render a page.

 

PageReference Types

A PageReference must be defined with a specific type. The type generates a unique URL format and provides attributes that apply to all pages of that type. The following types are supported.
1 Lightning Component (must implement lightning:isUrlAddressable) 

Example 

URL formate from the generateUrl method 

 

2 Knowledge Article

A page that interacts with a Knowledge Article record.

Exmaple 

URL formate from the generateUrl method 

3.Named Page

A standard page with a unique name. Only home, chatter, today, and dataAssessment are supported.

Example

URL formate from the generateUrl method 

4. Navigation Item Page

A page that displays the content mapped to a CustomTab. Visualforce tabs, Web tabs, Lightning Pages, and Lightning Component tabs are supported.

Example

URL formate from the generateUrl method 

5.Object Page

A page that interacts with an object in the org and supports standard actions for that object.The standard__objectPage type replaces the force:navigateToObjectHome and the force:navigateToList events.

Example

URL formate from the generateUrl method 

6.Record Page

A page that interacts with a record in the org and supports standard actions for that record.The standard__recordPage type replaces the force:navigateToSObject event

Example

URL formate from the generateUrl method 

7.Record Relationship Page

A page that interacts with a relationship on a particular record in the org. Only related lists are supported.The standard__recordRelationshipPage type replaces the force:navigateToRelatedList event.

Example 

URL formate from the generateUrl method