<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: German Quinteros</title>
    <description>The latest articles on DEV Community by German Quinteros (@gquinteros93).</description>
    <link>https://dev.to/gquinteros93</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F348650%2F8bbf8c68-30c0-4074-b690-abbb8e23be1a.jpeg</url>
      <title>DEV Community: German Quinteros</title>
      <link>https://dev.to/gquinteros93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gquinteros93"/>
    <language>en</language>
    <item>
      <title>Use the same route path for different Modules or Components in Angular</title>
      <dc:creator>German Quinteros</dc:creator>
      <pubDate>Wed, 01 Apr 2020 23:59:36 +0000</pubDate>
      <link>https://dev.to/gquinteros93/use-the-same-route-path-for-different-modules-or-components-in-angular-4g2i</link>
      <guid>https://dev.to/gquinteros93/use-the-same-route-path-for-different-modules-or-components-in-angular-4g2i</guid>
      <description>&lt;p&gt;One day I asked my self if I could use the same route path for different modules or components.&lt;/p&gt;

&lt;p&gt;Why would I need this feature?&lt;/p&gt;

&lt;p&gt;In my experience, I found this feature very useful in the following scenarios.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You want to use the same path for all users but depending on the user type/privileges which component/module should you load.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You have different versions of your web app and according to the server configuration, environment configuration and/or http responses, you can decide which version of the module/component to load.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Probably there are more reasons to use this feature, but these two are the reasons that came to my mind right now.&lt;/p&gt;

&lt;p&gt;Let’s set an imaginary scenario to show better this feature.&lt;/p&gt;

&lt;p&gt;We have a library web app where the customers could see the books available to use:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OjKp7kd3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/8vtJ2rt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OjKp7kd3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/8vtJ2rt.png" alt="Library Consumer" width="880" height="175"&gt;&lt;/a&gt;&lt;/p&gt;
Library Consumer



&lt;p&gt;On the other hand, we have the library manager, who can list and add the books available.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f2ZtH6Z1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/0OuOWdA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f2ZtH6Z1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/0OuOWdA.png" alt="Library Manager" width="880" height="382"&gt;&lt;/a&gt;&lt;/p&gt;
Library Manager



&lt;p&gt;Both types of users use the same url:&lt;br&gt;
&lt;strong&gt;&lt;a href="http://localhost:4200/books"&gt;http://localhost:4200/books&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;According to the user type (consumer or manager), we should decide what is the module that we need to load.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like we have just said we have two modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manager module&lt;/li&gt;
&lt;li&gt;Consumer module&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And we are going to add a third module: Book Handler Module.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A8EXb7Eq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/haAhMtc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A8EXb7Eq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/haAhMtc.png" alt="The three modules" width="306" height="310"&gt;&lt;/a&gt;&lt;/p&gt;
The three modules



&lt;p&gt;This last one (Book Handler Module) is going to be responsible to determine which module should we load. &lt;strong&gt;How is going to do that?&lt;/strong&gt; We are going to provide the ROUTES of the RouterModule using the &lt;a href="https://angular.io/api/core/FactorySansProvider#useFactory"&gt;useFactory provider of Angular&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8AGXmnQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/t4KQbhN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8AGXmnQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/t4KQbhN.png" alt="BookHandlerModule" width="880" height="644"&gt;&lt;/a&gt;&lt;/p&gt;
BookHandlerModule



&lt;p&gt;As you can see, the method “configBookHandlerRoutes” is going to determine what module we should load asking the AuthService if the user is authorized.&lt;/p&gt;

&lt;p&gt;The AuthService is just a service from where we get the role of the user (consumer or manager).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6eibMTU8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/7AaQflr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6eibMTU8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/7AaQflr.png" alt="AuthService" width="626" height="285"&gt;&lt;/a&gt;&lt;/p&gt;
AuthService



&lt;p&gt;Let’s see the application running:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d1dplc5p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/Qxk0K9M.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d1dplc5p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/Qxk0K9M.gif" alt="Library Consumer" width="880" height="471"&gt;&lt;/a&gt;&lt;/p&gt;
Library Consumer



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OjYlebZN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/Q8SXP14.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OjYlebZN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/Q8SXP14.gif" alt="Library Manager" width="880" height="471"&gt;&lt;/a&gt;&lt;/p&gt;
Library Manager



&lt;p&gt;&lt;strong&gt;Yes!!! It’s works!!!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/11FRmJRii0I8iA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/11FRmJRii0I8iA/giphy.gif" alt="Wait!!!" width="391" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a bug… In the login page if we change the answer value of “Are you the library manager?” after we had already navigated to the “/books” page, the application will only load the book page that had loaded the first time:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XhYHOe1q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/mwmG6xw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XhYHOe1q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/mwmG6xw.gif" alt="Bug" width="728" height="388"&gt;&lt;/a&gt;&lt;/p&gt;
Bug



&lt;p&gt;&lt;a href="https://i.giphy.com/media/s239QJIh56sRW/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/s239QJIh56sRW/giphy.gif" alt="Why?" width="480" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The method “configBookHandlerRoutes” used by the useFactory provider in the BookHandlerModule is only executed the first time we navigate to the “/books” path and after that, the Angular Router already knows which module needs to load:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8AGXmnQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/t4KQbhN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8AGXmnQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/t4KQbhN.png" alt="BookHandlerModule" width="880" height="644"&gt;&lt;/a&gt;&lt;/p&gt;
BookHandlerModule



&lt;h3&gt;
  
  
  How can we fix this bug?
&lt;/h3&gt;

&lt;p&gt;If the value of “authorized$” (property loaded in the login page) changes, we have to delete the “/books” path in the Router and add it again. Because of that when we navigate again to the “/books” path, the method “configBookHandlerRoutes” used by the useFactory provider will be executed again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U5BGzW4C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/BqW5CW7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U5BGzW4C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/BqW5CW7.png" alt="AuthService" width="880" height="406"&gt;&lt;/a&gt;&lt;/p&gt;
AuthService



&lt;h3&gt;
  
  
  Let’s see the application running after this change:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n33Cs16A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/Zos1mVn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n33Cs16A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/Zos1mVn.gif" alt="Works!!!" width="728" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s all!!!
&lt;/h2&gt;

&lt;p&gt;I hope this article would be helpful for you.&lt;/p&gt;

&lt;p&gt;If you have any doubt please comment this article and as soon as I can I will answer you.&lt;/p&gt;

&lt;p&gt;Here you have the code in &lt;a href="https://github.com/gquinteros93/same-path-for-different-modules"&gt;github&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/gquinteros93/same-path-for-different-modules"&gt;https://github.com/gquinteros93/same-path-for-different-modules&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/g9582DNuQppxC/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/g9582DNuQppxC/giphy.gif" alt="" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
    </item>
  </channel>
</rss>
