Angular default route always preloaded

Published

I have two routes :

  • home [ ” ]
  • about [ ‘about’ ]

When I go to /about directly with my browser, I’ve notice 2 things :

  • my home module is preloaded (whereas I’ve requested /about)
  • the URL is rewritted like this : /about -> / -> /about

app-routing.module.t

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', loadChildren: () => import('./routes/home/home.module').then(m => m.RoutesHomeModule), data: { preload: true } },
  { path: 'about', loadChildren: () => import('./routes/about/about.module').then(m => m.RoutesAboutModule) },
  { path: '**', redirectTo: '', pathMatch: 'full' }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy' }) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule { }

app.component.ts

import { Component, OnInit } from '@angular/core';
import { NavigationStart, Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less'],
})
export class AppComponent implements OnInit {

  constructor(
    private router: Router,
  ) { }

  ngOnInit(): void {
    this.router.events.subscribe(event => {
      if (event instanceof NavigationStart) {
        // outputs "/about" but NEVER "/"
        console.log(event.url);
      }
    });
  }

}

Angular : 13.0.2
Chrome : 96.0.4664.55
OS : MacOS 12.0.1

Source: Angular Questions

Published
Categorized as angular, lazy-loading Tagged ,

Answers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq