Sub topic 14
Navigating from a Lightning Web Component
This chapter assumes you know the Lightning URL patterns from the previous chapter, can read basic JavaScript, and have created a Lightning Web Component (LWC) before. You will use NavigationMixin to send users to the same destinations they reach by clicking: a list view and a record page.
Why not hard-code URLs?
You could set window.location to a Lightning URL. That breaks in Experience Cloud sites, the Salesforce mobile app, and console apps, because each has different URL rules. NavigationMixin takes a description of the destination, called a page reference, and the platform builds the right URL for the current context.
How NavigationMixin works
A page reference is a plain JavaScript object with three parts:
typenames the kind of destination, such asstandard__objectPageorstandard__recordPage.attributesidentify the destination, such as the object and record.stateholds optional extras, such as which list view to show.
You apply the mixin by wrapping LightningElement, then call this[NavigationMixin.Navigate] with a page reference.
import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class AccountNavigator extends NavigationMixin(LightningElement) {
openRecentAccounts() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Account',
actionName: 'list'
},
state: {
filterName: 'Recent'
}
});
}
openAccount(recordId) {
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: recordId,
objectApiName: 'Account',
actionName: 'view'
}
});
}
}Mapping page references to URLs
Each page reference corresponds to a URL pattern you already know.
| Page reference | Resulting Lightning URL |
|---|---|
| `standard__objectPage`, action `list`, state `filterName: 'Recent'` | `/lightning/o/Account/list?filterName=Recent` |
| `standard__recordPage`, action `view` | `/lightning/r/Account/001XXXXXXXXXXXXXXX/view` |
Calling the methods from a template
The two methods need something to trigger them. In the template, a button can call the list view method. For the record method, pass the record Id from a data- attribute.
handleOpenAccount(event) {
const recordId = event.currentTarget.dataset.id;
this.openAccount(recordId);
}The matching markup uses data-id={account.Id} on the element that has the click handler.
Navigating to a record in a new context
Navigate accepts an optional second argument, replace. When it is true, the new page replaces the current entry in the browser history, so the Back button skips it. The default is false.
this[NavigationMixin.Navigate](pageReference, true);Use this for redirects, for example after a component creates a record and immediately forwards the user to it.
Getting the URL without navigating
GenerateUrl returns a URL for a page reference as a promise. This is useful for href links, which let users open the destination in a new browser tab.
this[NavigationMixin.GenerateUrl]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
objectApiName: 'Account',
actionName: 'view'
}
}).then(url => {
this.accountUrl = url;
});Common errors
- Forgetting the mixin wrapper. Without
NavigationMixin(LightningElement),this[NavigationMixin.Navigate]is undefined and the call throws. - Using the wrong
type. A record page needsstandard__recordPage, notstandard__objectPage. - Passing a list view label instead of its API name in
filterName. Use a name likeRecentorAllAccounts, not the display label. - Omitting
objectApiNameoractionNameon a record page reference. Some contexts require them.
Exercise
A component must send users to the view page of an Account, and the Back button should not return to the component afterward. Which page reference type and which call do you use?
Show answer
Use a page reference with type standard__recordPage, attributes recordId, objectApiName: 'Account', and actionName: 'view'. Call this[NavigationMixin.Navigate](pageReference, true). The second argument true replaces the current history entry, so Back skips the component.
Exercise
You need an anchor tag whose href opens an Account record in a new browser tab. Which NavigationMixin method gives you the URL?
Show answer
this[NavigationMixin.GenerateUrl](pageReference). It returns a promise that resolves to the URL, which you store in a property and bind to href.