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;








3















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!










share|improve this question







New contributor



Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    3















    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!










    share|improve this question







    New contributor



    Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      3












      3








      3








      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!










      share|improve this question







      New contributor



      Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question







      New contributor



      Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question







      New contributor



      Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question






      New contributor



      Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked 8 hours ago









      Sebastian BelausteguiSebastian Belaustegui

      312




      312




      New contributor



      Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      Sebastian Belaustegui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          5














          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);







          share|improve this answer

























            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.









            draft saved

            draft discarded


















            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









            5














            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);







            share|improve this answer



























              5














              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);







              share|improve this answer

























                5












                5








                5







                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);







                share|improve this answer













                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);








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 8 hours ago









                John TowersJohn Towers

                2,4901918




                2,4901918




















                    Sebastian Belaustegui is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                    Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її