Sub topic 4
Applying NavigationMixin and calling Navigate
This chapter shows how to move a user from a Lightning Web Component to another page. It assumes you can build a basic LWC with an HTML template, a JavaScript class, and a metadata file. It also assumes you know how to wire up click handlers and how @api properties work. You should be able to read a record ID from a component and know what an object API name is.
Why navigation goes through a mixin
A component should not change window.location to move around Salesforce. Lightning Experience is a single-page application. A raw URL change reloads the whole app, discards state, and breaks in the mobile app and in Experience Cloud sites, where URL formats differ.
Salesforce supplies lightning/navigation instead. You describe the destination as an object, and the platform works out the right URL and navigation behavior for the container the component runs in. The entry point is NavigationMixin.
A mixin is a function that takes a base class and returns a new class that extends it with extra behavior. NavigationMixin(LightningElement) returns a class that is a LightningElement plus two navigation methods.
Applying the mixin
Three steps are needed:
- Import
NavigationMixinfromlightning/navigation. - Wrap the base class with it in the
extendsclause. - Call
this[NavigationMixin.Navigate](pageReference).
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'
}
});
}
}A template can trigger these handlers from buttons:
<template>
<lightning-button label="View account" onclick={handleView}></lightning-button>
<lightning-button label="New contact" onclick={handleNewContact}></lightning-button>
</template>Why the square brackets
NavigationMixin.Navigate is not a method name you can type after a dot. It is a key the mixin defines, and you use it as a computed property name. Writing this.Navigate(...) or this.NavigationMixin.Navigate(...) fails. Always write this[NavigationMixin.Navigate](...).
The two arguments
Navigate takes two arguments:
| Argument | Type | Meaning |
|---|---|---|
| `pageReference` | object | Describes the destination. Required. |
| `replace` | boolean | Optional. When `true`, the destination replaces the current entry in browser history. When omitted or `false`, a new entry is added. |
Navigate returns nothing. You cannot await it or chain a .then() to learn when navigation has finished. Run any work that must happen before the user leaves, such as a save, before you call Navigate.
The replace flag matters for flows where the Back button would send the user to a page that no longer makes sense. For example, after a user creates a record from a temporary form, you might not want Back to return to the empty form:
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class AfterCreate extends NavigationMixin(LightningElement) {
@api newRecordId;
goToCreatedRecord() {
this[NavigationMixin.Navigate](
{
type: 'standard__recordPage',
attributes: {
recordId: this.newRecordId,
actionName: 'view'
}
},
true
);
}
}What happens during a call
The sequence below shows how a click becomes a page change.
The component does not need to know whether it runs in Lightning Experience, an Experience Cloud site, or the Salesforce mobile app. The service picks the correct behavior.
Extending a class that already has a base
If your component extends a custom base class instead of LightningElement, wrap that class:
import { NavigationMixin } from 'lightning/navigation';
import BaseComponent from 'c/baseComponent';
export default class ChildComponent extends NavigationMixin(BaseComponent) {
// BaseComponent must itself extend LightningElement
}Apply the mixin once, at the class that needs it. Subclasses inherit the methods.
Common errors
The mixin is not applied
If you write extends LightningElement and still call this[NavigationMixin.Navigate], the browser throws a TypeError because the property does not exist on the class. The fix is to wrap the base class.
Calling Navigate where `this` is wrong
Methods referenced from a template, such as onclick={handleView}, are bound to the component. A plain function passed to setTimeout or a third-party callback is not bound. Use an arrow function so this still refers to the component:
setTimeout(() => {
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: { objectApiName: 'Account', actionName: 'home' }
});
}, 500);Navigating from a lifecycle hook
Avoid calling Navigate from constructor or connectedCallback. The page is still being built, and a redirect on load makes the Back button unreliable. Navigate in response to a user action or after an asynchronous result.
A malformed page reference
If type is misspelled or a required attribute is missing, navigation fails. Typical mistakes are standard__recordpage with the wrong case, or actionName: 'new' on a record page. The next chapters cover the structure of each page reference type.
Navigate compared with a plain link
| Approach | Behavior | Use when |
|---|---|---|
| `this[NavigationMixin.Navigate](ref)` | Navigates in the current app without a full reload | The user takes an action, such as clicking a button or finishing a save |
| `<a href={url}>` with a URL from `GenerateUrl` | Browser handles the link, so users can open it in a new tab | You want a real link users can copy or middle-click |
The second row is covered in a later chapter on GenerateUrl. Both approaches use the same page reference objects, so what you learn about them carries over.
Exercises
Exercise
A developer writes export default class Go extends LightningElement and calls this[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: 'Account', actionName: 'home' } }) in a click handler. The click throws a TypeError. What is wrong, and how do you fix it?
Show answer
The class does not apply the mixin, so this[NavigationMixin.Navigate] is undefined. Import NavigationMixin from lightning/navigation and change the class to extends NavigationMixin(LightningElement).
Exercise
After a user saves a new record from a form component, you navigate to the record page. You want the Back button to skip the form. What do you change in the Navigate call?
Show answer
Pass true as the second argument: this[NavigationMixin.Navigate](pageReference, true). This replaces the current history entry instead of adding a new one.