LWC - Best practice for routing?LWC-Jest installation failsError while creating Unlocked package version with LWC ComponentsImport ES modules in LWCLWC: Picklist without knowing recordTypeIdHow do I invoke a value change handler in LWC?Invoking LWC component from a plain URL - Read URL Parameter inside LWCUse Salesforce LWC Classes as User Interface ClassesLWC : passing parameter to getrecordLWC: How to create dynamic component in LWC?How do we manually call (child) LWC component method from (parent) Aura component?
When to remove insignificant variables?
Explain why a line can never intersect a plane in exactly two points.
Why tighten down in a criss-cross pattern?
Drawing people along with x and y axis
Trainee keeps missing deadlines for independent learning
Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?
What is the origin of Scooby-Doo's name?
Is there a way, while dragging, to "snap" to the nearest guide?
Can there be an UN resolution to remove a country from the UNSC?
Why aren't non-isolated DC-DC converters made for high wattage applications?
.NET executes a SQL query and Active Monitor shows multiple rows blocking each other
How is hair tissue mineral analysis performed?
Interaction between Leyline of Anticipation and Teferi, Time Raveler
What did River say when she woke from her proto-comatose state?
How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?
How many children?
Why does the Saturn V have standalone inter-stage rings?
Count All Possible Unique Combinations of Letters in a Word
How to make clear to people I don't want to answer their "Where are you from?" question?
Would it be a copyright violation if I made a character’s full name refer to a song?
How to find the last non zero element in every column throughout dataframe?
Is it illegal to withhold someone's passport and green card in California?
How long would it take to cross the Channel in 1890's?
How large would a mega structure have to be to host 1 billion people indefinitely?
LWC - Best practice for routing?
LWC-Jest installation failsError while creating Unlocked package version with LWC ComponentsImport ES modules in LWCLWC: Picklist without knowing recordTypeIdHow do I invoke a value change handler in LWC?Invoking LWC component from a plain URL - Read URL Parameter inside LWCUse Salesforce LWC Classes as User Interface ClassesLWC : passing parameter to getrecordLWC: How to create dynamic component in LWC?How do we manually call (child) LWC component method from (parent) Aura component?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I would like to know the best practice for routing in LWC. I mean, how to show a specific component on a specific URL, how to go to another route from a component, etc. If you can show me an example I would be grateful.
Thanks!
lightning-web-components
New contributor
add a comment |
I would like to know the best practice for routing in LWC. I mean, how to show a specific component on a specific URL, how to go to another route from a component, etc. If you can show me an example I would be grateful.
Thanks!
lightning-web-components
New contributor
add a comment |
I would like to know the best practice for routing in LWC. I mean, how to show a specific component on a specific URL, how to go to another route from a component, etc. If you can show me an example I would be grateful.
Thanks!
lightning-web-components
New contributor
I would like to know the best practice for routing in LWC. I mean, how to show a specific component on a specific URL, how to go to another route from a component, etc. If you can show me an example I would be grateful.
Thanks!
lightning-web-components
lightning-web-components
New contributor
New contributor
New contributor
asked 8 hours ago
Sebastian BelausteguiSebastian Belaustegui
312
312
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There's a demo up on GitHub from the TDX session on using LWC outside of Salesforce.
That uses Navigo for routing in the app module. Here's a slimmed down version of the routing from that example with some comments from my own experience trying to implement this:
import LightningElement, createElement, track from 'lwc';
import Navigo from 'navigo'; // import Navigo
export default class App extends LightningElement
// instantiate the router class
router = new Navigo(location.origin, false);
constructor()
super();
// define your routes. The await functions are for use with chunking but you could remove them and just do a normal import if all of your scripts are loaded at once
this.router.on(
'/podcasts': async () =>
const default: ViewPodcasts = await import(/* webpackChunkName: "view-podcasts" */ 'view/podcasts');
this.setPage('view-podcasts', ViewPodcasts);
,
'/podcasts/:id': async ( id ) =>
const default: ViewPodcast = await import(/* webpackChunkName: "view-podcast" */ 'view/podcast');
this.setPage('view-podcast', ViewPodcast,
podcastId: parseInt(id, 10),
);
,
'/discover': async () =>
const default: ViewDiscover = await import(/* webpackChunkName: "view-discover" */ 'view/discover');
this.setPage('view-discover', ViewDiscover);
,
'/categories/:id': async ( id ) =>
const default: PodcastList = await import(/* webpackChunkName: "view-category" */ 'view/category');
this.setPage('view-category', PodcastList,
categoryId: parseInt(id, 10),
);
,
);
const navigateToDefault = () =>
this.router.navigate('/podcasts');
;
this.router.notFound(navigateToDefault);
this.router.on(navigateToDefault);
renderedCallback()
// Resolve the current view only after the container has rendered
if (!this.isRendered)
this.isRendered = true;
this.router.resolve();
setPage(tagName, component, props = )
// when a route is called, create the related component and insert it into the DOM in whatever container you want
const el = createElement(tagName,
is: component,
fallback: false,
);
Object.assign(el, props);
// Remove previous components from the container if necessary
const container = this.template.querySelector('.container');
while (container.firstChild)
container.removeChild(container.firstChild);
container.appendChild(el);
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sebastian Belaustegui is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f266551%2flwc-best-practice-for-routing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There's a demo up on GitHub from the TDX session on using LWC outside of Salesforce.
That uses Navigo for routing in the app module. Here's a slimmed down version of the routing from that example with some comments from my own experience trying to implement this:
import LightningElement, createElement, track from 'lwc';
import Navigo from 'navigo'; // import Navigo
export default class App extends LightningElement
// instantiate the router class
router = new Navigo(location.origin, false);
constructor()
super();
// define your routes. The await functions are for use with chunking but you could remove them and just do a normal import if all of your scripts are loaded at once
this.router.on(
'/podcasts': async () =>
const default: ViewPodcasts = await import(/* webpackChunkName: "view-podcasts" */ 'view/podcasts');
this.setPage('view-podcasts', ViewPodcasts);
,
'/podcasts/:id': async ( id ) =>
const default: ViewPodcast = await import(/* webpackChunkName: "view-podcast" */ 'view/podcast');
this.setPage('view-podcast', ViewPodcast,
podcastId: parseInt(id, 10),
);
,
'/discover': async () =>
const default: ViewDiscover = await import(/* webpackChunkName: "view-discover" */ 'view/discover');
this.setPage('view-discover', ViewDiscover);
,
'/categories/:id': async ( id ) =>
const default: PodcastList = await import(/* webpackChunkName: "view-category" */ 'view/category');
this.setPage('view-category', PodcastList,
categoryId: parseInt(id, 10),
);
,
);
const navigateToDefault = () =>
this.router.navigate('/podcasts');
;
this.router.notFound(navigateToDefault);
this.router.on(navigateToDefault);
renderedCallback()
// Resolve the current view only after the container has rendered
if (!this.isRendered)
this.isRendered = true;
this.router.resolve();
setPage(tagName, component, props = )
// when a route is called, create the related component and insert it into the DOM in whatever container you want
const el = createElement(tagName,
is: component,
fallback: false,
);
Object.assign(el, props);
// Remove previous components from the container if necessary
const container = this.template.querySelector('.container');
while (container.firstChild)
container.removeChild(container.firstChild);
container.appendChild(el);
add a comment |
There's a demo up on GitHub from the TDX session on using LWC outside of Salesforce.
That uses Navigo for routing in the app module. Here's a slimmed down version of the routing from that example with some comments from my own experience trying to implement this:
import LightningElement, createElement, track from 'lwc';
import Navigo from 'navigo'; // import Navigo
export default class App extends LightningElement
// instantiate the router class
router = new Navigo(location.origin, false);
constructor()
super();
// define your routes. The await functions are for use with chunking but you could remove them and just do a normal import if all of your scripts are loaded at once
this.router.on(
'/podcasts': async () =>
const default: ViewPodcasts = await import(/* webpackChunkName: "view-podcasts" */ 'view/podcasts');
this.setPage('view-podcasts', ViewPodcasts);
,
'/podcasts/:id': async ( id ) =>
const default: ViewPodcast = await import(/* webpackChunkName: "view-podcast" */ 'view/podcast');
this.setPage('view-podcast', ViewPodcast,
podcastId: parseInt(id, 10),
);
,
'/discover': async () =>
const default: ViewDiscover = await import(/* webpackChunkName: "view-discover" */ 'view/discover');
this.setPage('view-discover', ViewDiscover);
,
'/categories/:id': async ( id ) =>
const default: PodcastList = await import(/* webpackChunkName: "view-category" */ 'view/category');
this.setPage('view-category', PodcastList,
categoryId: parseInt(id, 10),
);
,
);
const navigateToDefault = () =>
this.router.navigate('/podcasts');
;
this.router.notFound(navigateToDefault);
this.router.on(navigateToDefault);
renderedCallback()
// Resolve the current view only after the container has rendered
if (!this.isRendered)
this.isRendered = true;
this.router.resolve();
setPage(tagName, component, props = )
// when a route is called, create the related component and insert it into the DOM in whatever container you want
const el = createElement(tagName,
is: component,
fallback: false,
);
Object.assign(el, props);
// Remove previous components from the container if necessary
const container = this.template.querySelector('.container');
while (container.firstChild)
container.removeChild(container.firstChild);
container.appendChild(el);
add a comment |
There's a demo up on GitHub from the TDX session on using LWC outside of Salesforce.
That uses Navigo for routing in the app module. Here's a slimmed down version of the routing from that example with some comments from my own experience trying to implement this:
import LightningElement, createElement, track from 'lwc';
import Navigo from 'navigo'; // import Navigo
export default class App extends LightningElement
// instantiate the router class
router = new Navigo(location.origin, false);
constructor()
super();
// define your routes. The await functions are for use with chunking but you could remove them and just do a normal import if all of your scripts are loaded at once
this.router.on(
'/podcasts': async () =>
const default: ViewPodcasts = await import(/* webpackChunkName: "view-podcasts" */ 'view/podcasts');
this.setPage('view-podcasts', ViewPodcasts);
,
'/podcasts/:id': async ( id ) =>
const default: ViewPodcast = await import(/* webpackChunkName: "view-podcast" */ 'view/podcast');
this.setPage('view-podcast', ViewPodcast,
podcastId: parseInt(id, 10),
);
,
'/discover': async () =>
const default: ViewDiscover = await import(/* webpackChunkName: "view-discover" */ 'view/discover');
this.setPage('view-discover', ViewDiscover);
,
'/categories/:id': async ( id ) =>
const default: PodcastList = await import(/* webpackChunkName: "view-category" */ 'view/category');
this.setPage('view-category', PodcastList,
categoryId: parseInt(id, 10),
);
,
);
const navigateToDefault = () =>
this.router.navigate('/podcasts');
;
this.router.notFound(navigateToDefault);
this.router.on(navigateToDefault);
renderedCallback()
// Resolve the current view only after the container has rendered
if (!this.isRendered)
this.isRendered = true;
this.router.resolve();
setPage(tagName, component, props = )
// when a route is called, create the related component and insert it into the DOM in whatever container you want
const el = createElement(tagName,
is: component,
fallback: false,
);
Object.assign(el, props);
// Remove previous components from the container if necessary
const container = this.template.querySelector('.container');
while (container.firstChild)
container.removeChild(container.firstChild);
container.appendChild(el);
There's a demo up on GitHub from the TDX session on using LWC outside of Salesforce.
That uses Navigo for routing in the app module. Here's a slimmed down version of the routing from that example with some comments from my own experience trying to implement this:
import LightningElement, createElement, track from 'lwc';
import Navigo from 'navigo'; // import Navigo
export default class App extends LightningElement
// instantiate the router class
router = new Navigo(location.origin, false);
constructor()
super();
// define your routes. The await functions are for use with chunking but you could remove them and just do a normal import if all of your scripts are loaded at once
this.router.on(
'/podcasts': async () =>
const default: ViewPodcasts = await import(/* webpackChunkName: "view-podcasts" */ 'view/podcasts');
this.setPage('view-podcasts', ViewPodcasts);
,
'/podcasts/:id': async ( id ) =>
const default: ViewPodcast = await import(/* webpackChunkName: "view-podcast" */ 'view/podcast');
this.setPage('view-podcast', ViewPodcast,
podcastId: parseInt(id, 10),
);
,
'/discover': async () =>
const default: ViewDiscover = await import(/* webpackChunkName: "view-discover" */ 'view/discover');
this.setPage('view-discover', ViewDiscover);
,
'/categories/:id': async ( id ) =>
const default: PodcastList = await import(/* webpackChunkName: "view-category" */ 'view/category');
this.setPage('view-category', PodcastList,
categoryId: parseInt(id, 10),
);
,
);
const navigateToDefault = () =>
this.router.navigate('/podcasts');
;
this.router.notFound(navigateToDefault);
this.router.on(navigateToDefault);
renderedCallback()
// Resolve the current view only after the container has rendered
if (!this.isRendered)
this.isRendered = true;
this.router.resolve();
setPage(tagName, component, props = )
// when a route is called, create the related component and insert it into the DOM in whatever container you want
const el = createElement(tagName,
is: component,
fallback: false,
);
Object.assign(el, props);
// Remove previous components from the container if necessary
const container = this.template.querySelector('.container');
while (container.firstChild)
container.removeChild(container.firstChild);
container.appendChild(el);
answered 8 hours ago
John TowersJohn Towers
2,4901918
2,4901918
add a comment |
add a comment |
Sebastian Belaustegui is a new contributor. Be nice, and check out our Code of Conduct.
Sebastian Belaustegui is a new contributor. Be nice, and check out our Code of Conduct.
Sebastian Belaustegui is a new contributor. Be nice, and check out our Code of Conduct.
Sebastian Belaustegui is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f266551%2flwc-best-practice-for-routing%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown