Sub topic 4
Applying NavigationMixin and calling Navigate
This chapter shows how to add NavigationMixin to a component and move the user to another page. It assumes you know the basic structure of a Lightning Web Component (template, JavaScript class, metadata file), the @api decorator, and Salesforce record IDs.
What NavigationMixin does
A component cannot change the browser location directly. Salesforce runs components inside different containers, such as Lightning Experience, the Salesforce mobile app, and Experience Cloud sites. Each container builds its URLs differently.
NavigationMixin solves this. You describe the destination as a page reference object, and the platform turns it into the right navigation for the current container. Your code stays the same everywhere.
Applying the mixin
Import NavigationMixin from lightning/navigation. Wrap LightningElement with it in the extends clause. The mixin adds two methods to your class, Navigate and GenerateUrl. Both are stored under symbol keys, so you call them with bracket syntax.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class OpenRecord extends NavigationMixin(LightningElement) {
@api recordId;
handleView() {
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: this.recordId,
objectApiName: 'Account',
actionName: 'view'
}
});
}
handleNewContact() {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: {
objectApiName: 'Contact',
actionName: 'new'
}
});
}
}Reading the call
this[NavigationMixin.Navigate](pageReference) takes one required argument, a page reference. A page reference is a plain object with a type and an attributes object. The type says what kind of page you want. The attributes identify the exact page.
handleViewopens the record page for the current account. It usesstandard__recordPagewith a record ID, an object name, and theviewaction.handleNewContactopens the New Contact form. It usesstandard__objectPagewith thenewaction.
The call returns nothing useful. It starts the navigation and your code continues.
How the call flows
Common errors
- Calling
this.Navigate(...). The method is not a normal property. Usethis[NavigationMixin.Navigate](...). - Forgetting the wrapper. If your class only extends
LightningElement,this[NavigationMixin.Navigate]isundefinedand the call throws aTypeError. - Misspelled
type. A wrong or missing type, such asstandard_recordPagewith one underscore, means the page does not open. - Missing attributes. A record page needs
recordIdandactionName. A page reference without the required attributes fails to resolve.
Exercise
Your component class is declared as export default class Nav extends LightningElement, and calling this[NavigationMixin.Navigate](ref) throws a TypeError. What is the fix?
Show answer
Wrap the base class with the mixin: export default class Nav extends NavigationMixin(LightningElement). Without the wrapper, the Navigate method does not exist on the component.
Exercise
Write a page reference that opens the Account list home page.
Show answer
{ type: 'standard__objectPage', attributes: { objectApiName: 'Account', actionName: 'home' } }