Angular: Can't navigate to children routes with UrlMatcher












0















Preface:
Part of my URL strategy has an optional parameter. Meaning, the URL path could start with a region code and language code or just a language code. These codes are then proceeded by the actual app route.



/us/eng --> Home page
/us/eng/about-us --> About Us page
/eng/about-us --> About Us page


I am trying to use a UrlMatcher to accomplish the the optional region code in the URL.



My problem is, the Home page is always displayed. It never shows the About Us page or any other child route.



partial app.routes.ts



export function baseRouteMatcher(url: UrlSegment) {
const posParams: { [key: string]: UrlSegment } = {};
const consumed: UrlSegment = url;
if (url[0].path.length === 2 && url[1].path.length === 3) {
posParams.region = url[0];
posParams.lang = url[1];
} else if(url[0].path.length === 3) {
posParams.region = new UrlSegment('world', {});
posParams.lang = url[0];
}
return ({consumed, posParams});
}

export const appRoute = {
name: 'app',
matcher: baseRouteMatcher,
children: [
{ path: 'terms-of-service', component: ContentComponent },
{ path: 'privacy-policy', component: ContentComponent },
{ path: 'about-us', loadChildren: './about-us/about-us.module#AboutUsModule' },
{ path: '', component: HomeComponent },
]
};


It's quite possible I completely misunderstand how UrlMatcher functions. It's hard finding complex examples. Any help is appreciated!










share|improve this question



























    0















    Preface:
    Part of my URL strategy has an optional parameter. Meaning, the URL path could start with a region code and language code or just a language code. These codes are then proceeded by the actual app route.



    /us/eng --> Home page
    /us/eng/about-us --> About Us page
    /eng/about-us --> About Us page


    I am trying to use a UrlMatcher to accomplish the the optional region code in the URL.



    My problem is, the Home page is always displayed. It never shows the About Us page or any other child route.



    partial app.routes.ts



    export function baseRouteMatcher(url: UrlSegment) {
    const posParams: { [key: string]: UrlSegment } = {};
    const consumed: UrlSegment = url;
    if (url[0].path.length === 2 && url[1].path.length === 3) {
    posParams.region = url[0];
    posParams.lang = url[1];
    } else if(url[0].path.length === 3) {
    posParams.region = new UrlSegment('world', {});
    posParams.lang = url[0];
    }
    return ({consumed, posParams});
    }

    export const appRoute = {
    name: 'app',
    matcher: baseRouteMatcher,
    children: [
    { path: 'terms-of-service', component: ContentComponent },
    { path: 'privacy-policy', component: ContentComponent },
    { path: 'about-us', loadChildren: './about-us/about-us.module#AboutUsModule' },
    { path: '', component: HomeComponent },
    ]
    };


    It's quite possible I completely misunderstand how UrlMatcher functions. It's hard finding complex examples. Any help is appreciated!










    share|improve this question

























      0












      0








      0








      Preface:
      Part of my URL strategy has an optional parameter. Meaning, the URL path could start with a region code and language code or just a language code. These codes are then proceeded by the actual app route.



      /us/eng --> Home page
      /us/eng/about-us --> About Us page
      /eng/about-us --> About Us page


      I am trying to use a UrlMatcher to accomplish the the optional region code in the URL.



      My problem is, the Home page is always displayed. It never shows the About Us page or any other child route.



      partial app.routes.ts



      export function baseRouteMatcher(url: UrlSegment) {
      const posParams: { [key: string]: UrlSegment } = {};
      const consumed: UrlSegment = url;
      if (url[0].path.length === 2 && url[1].path.length === 3) {
      posParams.region = url[0];
      posParams.lang = url[1];
      } else if(url[0].path.length === 3) {
      posParams.region = new UrlSegment('world', {});
      posParams.lang = url[0];
      }
      return ({consumed, posParams});
      }

      export const appRoute = {
      name: 'app',
      matcher: baseRouteMatcher,
      children: [
      { path: 'terms-of-service', component: ContentComponent },
      { path: 'privacy-policy', component: ContentComponent },
      { path: 'about-us', loadChildren: './about-us/about-us.module#AboutUsModule' },
      { path: '', component: HomeComponent },
      ]
      };


      It's quite possible I completely misunderstand how UrlMatcher functions. It's hard finding complex examples. Any help is appreciated!










      share|improve this question














      Preface:
      Part of my URL strategy has an optional parameter. Meaning, the URL path could start with a region code and language code or just a language code. These codes are then proceeded by the actual app route.



      /us/eng --> Home page
      /us/eng/about-us --> About Us page
      /eng/about-us --> About Us page


      I am trying to use a UrlMatcher to accomplish the the optional region code in the URL.



      My problem is, the Home page is always displayed. It never shows the About Us page or any other child route.



      partial app.routes.ts



      export function baseRouteMatcher(url: UrlSegment) {
      const posParams: { [key: string]: UrlSegment } = {};
      const consumed: UrlSegment = url;
      if (url[0].path.length === 2 && url[1].path.length === 3) {
      posParams.region = url[0];
      posParams.lang = url[1];
      } else if(url[0].path.length === 3) {
      posParams.region = new UrlSegment('world', {});
      posParams.lang = url[0];
      }
      return ({consumed, posParams});
      }

      export const appRoute = {
      name: 'app',
      matcher: baseRouteMatcher,
      children: [
      { path: 'terms-of-service', component: ContentComponent },
      { path: 'privacy-policy', component: ContentComponent },
      { path: 'about-us', loadChildren: './about-us/about-us.module#AboutUsModule' },
      { path: '', component: HomeComponent },
      ]
      };


      It's quite possible I completely misunderstand how UrlMatcher functions. It's hard finding complex examples. Any help is appreciated!







      javascript angular typescript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 21:33









      blbakerblbaker

      9212




      9212
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It's true. I didn't understand how UrlMatcher worked. Two things were happening:




          1. I was consuming part of the url every time the matcher function was run (as opposed to returning null when I didn't need to).

          2. I was also consuming ALL segments every time the matcher function was run instead of just the parts I needed to. This resulted in getting the HomeComponent each time.


          Here's the working code:



          export function baseRouteMatcher(segments) {

          // If we don't have any segments
          if (!segments[0]) {
          return null;
          }

          // If we only have a language, consume just the first segment
          // Else if we have a region and a language, consume first two segments
          if (segments[0].path.length === 3) {
          return { consumed: segments.slice(0, 1), posParams: { region: 'world', lang: segments[0].path } };
          } else if (segments[0].path.length === 2 && segments[1].path.length === 3) {
          return { consumed: segments.slice(0, 2), posParams: { region: segments[0].path, lang: segments[1].path } };
          }

          // The segments don't contain a region or language, don't consume anything
          return null;
          }





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328222%2fangular-cant-navigate-to-children-routes-with-urlmatcher%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









            0














            It's true. I didn't understand how UrlMatcher worked. Two things were happening:




            1. I was consuming part of the url every time the matcher function was run (as opposed to returning null when I didn't need to).

            2. I was also consuming ALL segments every time the matcher function was run instead of just the parts I needed to. This resulted in getting the HomeComponent each time.


            Here's the working code:



            export function baseRouteMatcher(segments) {

            // If we don't have any segments
            if (!segments[0]) {
            return null;
            }

            // If we only have a language, consume just the first segment
            // Else if we have a region and a language, consume first two segments
            if (segments[0].path.length === 3) {
            return { consumed: segments.slice(0, 1), posParams: { region: 'world', lang: segments[0].path } };
            } else if (segments[0].path.length === 2 && segments[1].path.length === 3) {
            return { consumed: segments.slice(0, 2), posParams: { region: segments[0].path, lang: segments[1].path } };
            }

            // The segments don't contain a region or language, don't consume anything
            return null;
            }





            share|improve this answer






























              0














              It's true. I didn't understand how UrlMatcher worked. Two things were happening:




              1. I was consuming part of the url every time the matcher function was run (as opposed to returning null when I didn't need to).

              2. I was also consuming ALL segments every time the matcher function was run instead of just the parts I needed to. This resulted in getting the HomeComponent each time.


              Here's the working code:



              export function baseRouteMatcher(segments) {

              // If we don't have any segments
              if (!segments[0]) {
              return null;
              }

              // If we only have a language, consume just the first segment
              // Else if we have a region and a language, consume first two segments
              if (segments[0].path.length === 3) {
              return { consumed: segments.slice(0, 1), posParams: { region: 'world', lang: segments[0].path } };
              } else if (segments[0].path.length === 2 && segments[1].path.length === 3) {
              return { consumed: segments.slice(0, 2), posParams: { region: segments[0].path, lang: segments[1].path } };
              }

              // The segments don't contain a region or language, don't consume anything
              return null;
              }





              share|improve this answer




























                0












                0








                0







                It's true. I didn't understand how UrlMatcher worked. Two things were happening:




                1. I was consuming part of the url every time the matcher function was run (as opposed to returning null when I didn't need to).

                2. I was also consuming ALL segments every time the matcher function was run instead of just the parts I needed to. This resulted in getting the HomeComponent each time.


                Here's the working code:



                export function baseRouteMatcher(segments) {

                // If we don't have any segments
                if (!segments[0]) {
                return null;
                }

                // If we only have a language, consume just the first segment
                // Else if we have a region and a language, consume first two segments
                if (segments[0].path.length === 3) {
                return { consumed: segments.slice(0, 1), posParams: { region: 'world', lang: segments[0].path } };
                } else if (segments[0].path.length === 2 && segments[1].path.length === 3) {
                return { consumed: segments.slice(0, 2), posParams: { region: segments[0].path, lang: segments[1].path } };
                }

                // The segments don't contain a region or language, don't consume anything
                return null;
                }





                share|improve this answer















                It's true. I didn't understand how UrlMatcher worked. Two things were happening:




                1. I was consuming part of the url every time the matcher function was run (as opposed to returning null when I didn't need to).

                2. I was also consuming ALL segments every time the matcher function was run instead of just the parts I needed to. This resulted in getting the HomeComponent each time.


                Here's the working code:



                export function baseRouteMatcher(segments) {

                // If we don't have any segments
                if (!segments[0]) {
                return null;
                }

                // If we only have a language, consume just the first segment
                // Else if we have a region and a language, consume first two segments
                if (segments[0].path.length === 3) {
                return { consumed: segments.slice(0, 1), posParams: { region: 'world', lang: segments[0].path } };
                } else if (segments[0].path.length === 2 && segments[1].path.length === 3) {
                return { consumed: segments.slice(0, 2), posParams: { region: segments[0].path, lang: segments[1].path } };
                }

                // The segments don't contain a region or language, don't consume anything
                return null;
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 7 '18 at 19:39

























                answered Dec 7 '18 at 17:32









                blbakerblbaker

                9212




                9212
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f53328222%2fangular-cant-navigate-to-children-routes-with-urlmatcher%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma