简体   繁体   中英

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?

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 {}

我看到stackblitz,不需要在app.routing.module.ts中导入booklist组件和user组件,请删除并再次测试,也让我知道

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM