0

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?

2
  • You need to use routerLink instead of href in the anchor tags Commented Sep 2, 2020 at 21:41
  • Hi @Clashsoft I tried that but is not helping Commented Sep 2, 2020 at 21:43

2 Answers 2

3

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 {}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @AbolfazlR I tried your suggestion but that's not helping
0

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

2 Comments

Actually above answer fixed it I was missing router import for user module
Good, also you can consider above answer as Note then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.