lazy-loading - 具有子组件默认路由的 Angular 延迟加载模块

标签 lazy-loading angular-router angular9

Angular 9。子组件未使用延迟加载的模块组件进行加载。这是我的代码。

app-routing.module.ts

  {
    path: '',
    redirectTo: '/auth',
    pathMatch: 'full'
  },
  {
    path: 'auth',
    loadChildren: () => import('./pages/auth/auth.module').then(m => m.AuthModule),
  }

auth.module.ts

  {
    path: '',
    component: AuthRootComponent,
    children: [
      { path: 'login', component: LoginComponent},
      { path: 'forgot', component: ForgotPasswordComponent},
      { path: '', redirectTo: 'login', pathMatch: 'full' },
    ]
  }

当我使用localhost:4200/时,它会将我重定向到localhose:4200/auth。它不加载登录组件。

但是当我在浏览器中点击网址(localhost:4200/auth)时,它将加载登录组件,新的网址将是所需的网址,即 localhost:4200/auth/login.

请告诉我,当加载 auth 模块并且 url 为 localhost:4200 时,为什么它不从子数组加载登录名? URL 应该是 localhost:4200/auth/login 但现在获取 url localhost:4200/auth

最佳答案

我认为您必须将 redirectTo: '/auth' 更改为 redirectTo: 'auth'。这是因为当您使用绝对重定向(例如 /auth)时,仅允许一个重定向操作,在本例中是转到 /的重定向操作auth,这意味着任何其他重定向(例如 redirectTo: 'login')都将被忽略。

Here's它检查是否有任何绝对重定向:

if (allowRedirects && this.allowRedirects) {
    return this.expandSegmentAgainstRouteUsingRedirect(
      ngModule, segmentGroup, routes, route, paths, outlet);
}

here它指出在绝对重定向之后,不能有任何其他重定向:

if (e instanceof AbsoluteRedirect) {
  // after an absolute redirect we do not apply any more redirects!
  this.allowRedirects = false;
  // we need to run matching, so we can fetch all lazy-loaded modules
  return this.match(e.urlTree);
}

绝对重定向与相对重定向

摘 self 的 GH repo

redirectTo: 'foo/bar'redirectTo: '/foo/bar' 之间的区别在于:

  • /- 将从根开始(第一个最外层的路线数组)
  • `` - 将从当前路由所在数组的第一条路由开始搜索

关于lazy-loading - 具有子组件默认路由的 Angular 延迟加载模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62738338/

相关文章:

angular - 无法将第二个数组绑定(bind)到 angular-dual-listbox

angular - 如何修复 "Cannot set property of undefined"?

angular - 从父级访问路由器导出组件

Angular Router 在以编程方式导航时,旧组件保留在 dom 中

带有@Injectable() 的 Angular 9 BaseComponent

android - 如何在惰性列表中使图像更大(高度)

angular - 在第一个值发射后,主体从数组中失去观察者。 Angular RouteReuseStrategy 表编辑表单通信

hibernate - Spring 和 hibernate : load lazy collection

Hibernate 批量获取到延迟加载

Angular2 不适用于延迟模块加载的自定义重用策略