Sandy Pm
Sandy Pm

Reputation: 43

Angular Lazy Loading loading same route again

https://stackblitz.com/github/sandypm2020/BookStore

<ul class="leftNav">
  <li>
    <a routerLink="/books">Books</a>
  </li>
  <li>
    <a routerLink="/users">Users</a>
  </li>
</ul>
const routes: Routes = [
  {
    path: 'books',
    loadChildren: () => import('./book/book.module').then((m) => m.BookModule),
  },
  {
    path: 'users',
    loadChildren: () => import('./user/user.module').then((m) => m.UserModule),
  },
  { path: '', pathMatch: 'full', redirectTo: 'books' },
];

Guys I want to use lazy loading I have following routes and URLs. But for some reason when I go to user it loads books route again can someone help why that is happening?

Upvotes: 0

Views: 160

Answers (2)

LittleFuerry
LittleFuerry

Reputation: 43

I saw stackblitz, No need to import booklist component and user component in app.routing.module.ts, please remove and test again, let me know as well

Upvotes: 0

Abolfazl Roshanzamir
Abolfazl Roshanzamir

Reputation: 14802

First: change href to routerLink like the following :

<ul class="leftNav">
        <li>
          <a routerLink="/books">Books</a>
        </li>
        <li>
          <a routerLink="/users">Users</a>
        </li>
  </ul>

Then go to UserModule and add the routes to import section like the following :

@NgModule({
  declarations: [UserListComponent, AddUserComponent, UserEditComponent],
  imports: [CommonModule, RouterModule.forChild(routes)],
})
export class UserModule {}

Upvotes: 3

Related Questions