<?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>Forem: Krijesh</title>
    <description>The latest articles on Forem by Krijesh (@pvkrijesh).</description>
    <link>https://forem.com/pvkrijesh</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%2F1273818%2F0537b015-980f-4dc8-8116-1c5ad3c6a019.png</url>
      <title>Forem: Krijesh</title>
      <link>https://forem.com/pvkrijesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pvkrijesh"/>
    <language>en</language>
    <item>
      <title>Angular Complex Dynamic Routing!</title>
      <dc:creator>Krijesh</dc:creator>
      <pubDate>Fri, 09 Feb 2024 08:35:51 +0000</pubDate>
      <link>https://forem.com/pvkrijesh/angular-complex-dynamic-routing-34ng</link>
      <guid>https://forem.com/pvkrijesh/angular-complex-dynamic-routing-34ng</guid>
      <description>&lt;p&gt;Angular’s flexibility truly shines in complex scenarios!Woow!!! Recently, I encountered a particularly intricate situation while working on dynamic routing in Angular.&lt;/p&gt;

&lt;p&gt;Picture this: Launch your Angular application at &lt;a href="http://localhost:4200/mydomain"&gt;http://localhost:4200/mydomain&lt;/a&gt;, and here, ‘mydomain’ comes dynamically from an event listener. Sometimes, you might even receive both a main domain and a subdomain, requiring your Angular application to route dynamically like so: &lt;a href="http://localhost:4200/mydomain/subdomain"&gt;http://localhost:4200/mydomain/subdomain&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, how did I tackle this challenge? Started scratch my head!!! :(&lt;/p&gt;

&lt;p&gt;Initially, I considered using APP Initializer, but it turned out to have some issues.&lt;/p&gt;

&lt;p&gt;So, what’s the next step?&lt;/p&gt;

&lt;p&gt;The data from the event listener is received in the app component and stored in the Ngrx store.&lt;br&gt;
But how do I handle redirection based on this data? Enter the loading component with a ‘showLoading’ variable, and I started subscribing to the store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myData$ = this.store.select(selectorName);
mySubscription = myData$.subscribe(data =&amp;gt; {
  const { isLoaded, DomainA, SubDomainA } = data;
  if (isLoaded) {
    showLoading = false;
    if (SubDomainA !== '')
      this.router.navigateByUrl(`/${DomainA}/${SubDomainA}`);
    else
      this.router.navigateByUrl(`/${DomainA}`);
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, navigation to the specific route is achieved. Now, the challenge is to use this dynamic value in the app routing.&lt;/p&gt;

&lt;p&gt;In the app-routing.module.ts file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const Routes = [
  {
    path: ':dynamicPath',
    loadChildren: () =&amp;gt;
      import('./DomainA.module').then(mod =&amp;gt; mod.DomainAModule),
    resolve: {
      dynamicPath: myResolver
    }
  },
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next I created a myResolver.ts file to handle the resolution and return the data from the store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resolve(): string {
  let myDatafromStore;
  let dynamicPath;
  myDatafromStore = this.store.select(SelectorName);
  const { isLoaded, DomainA, SubDomainA } = data;
  if (isLoaded) {
    if (SubDomainA !== '')
      dynamicPath = `/${DomainA}/${SubDomainA}`;
    else
      dynamicPath = `${DomainA}`;
  }
  return dynamicPath;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, Angular’s dynamic routing, coupled with the power of Ngrx store and resolver, allows for seamless handling of complex scenarios.&lt;br&gt;
There are different ways to achieve this scenario but as per my requirement, this looks good and clean!&lt;br&gt;
Happy Coding!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>routing</category>
      <category>advancedrouting</category>
      <category>dynamicrouting</category>
    </item>
  </channel>
</rss>
