How do you navigate between pages and records in LWC using the lightning/navigation service?
Answer
Basics: Hard-coded URLs break across contexts (Lightning Experience, mobile, Experience Cloud). A page reference is a platform-neutral description of where to go, and the navigation service converts it to the right URL.
How it works:
import { NavigationMixin } from 'lightning/navigation';export default class X extends NavigationMixin(LightningElement).- Call
this[NavigationMixin.Navigate]({ type, attributes, state })to navigate, orthis[NavigationMixin.GenerateUrl](ref).then(url => ...)to get a link for an anchor. - Common types:
standard__recordPage(actionName: view, edit, clone),standard__objectPage(actionName: home, list, new),standard__namedPage(for examplehome),standard__navItemPage(custom tab),standard__webPage(external URL),standard__app,standard__recordRelationshipPage, andcomm__namedPagein Experience Cloud. - To read incoming parameters, wire
CurrentPageReferenceand readstate(custom parameters must be prefixedc__).
Advanced / deep dive: state carries URL parameters for list view filters (filterName) or custom values (c__orderId). For prefilled new records use actionName: 'new' with defaultFieldValues encoded by encodeDefaultFieldValues from lightning/pageReferenceUtils. The optional second argument true replaces the current history entry rather than adding one. LWCs cannot be navigated to directly with , which targets Aura components; navigating to an LWC means putting it on a Lightning page or tab () or wrapping it. Experience Cloud sites support a different set of page types and URL formats, so verify per site type. Navigation from inside a Flow screen uses the flow navigation events () rather than the navigation mixin.