角度lazyLoading从模块1到模块2,但反之亦然,为什么?

时间:2019-03-06 13:05:52

标签: angular module routing routes lazy-loading

我有一个Angular项目。 在我的一个模块中,我有一条以以下方式配置的路由:

const routes: Routes = [{
  path: '',
  component: PagesComponent,
  children: [
    {
      path: 'dashboard',
      loadChildren: 'app/pages/dashboard/dashboard.module#DashboardModule'
    },
    {
       path: 'members',
       loadChildren: 'app/pages/members/members.module#MembersModule'
     },
    { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: '**', redirectTo: 'dashboard', pathMatch: 'full' },
  ],
}];

路由运行良好,但是延迟加载存在问题。

如果我导航到URL /dashboard并单击指向/members/add的routerLink,则正在延迟加载MembersModule-一切正常。 但是,如果我访问URL /members/add,然后单击指向/dashboard的routerLink,页面将立即打开,没有任何延迟加载,就像已经加载一样。

我不知道为什么会这样。

更新:

最高级别是 AppRoutingModule

export const routes: Routes = [
  {
    path: 'auth',
    canActivate: [LoggedOutGuard],
    loadChildren: 'app/auth/auth.module#AuthModule'
  },
  {
    path: 'pages',
    canActivate: [LoggedInGuard],
    loadChildren: 'app/pages/pages.module#PagesModule'
  },
  { path: '', redirectTo: 'pages', pathMatch: 'full' },
  { path: '**', redirectTo: 'pages' }
];

const config: ExtraOptions = {
  useHash: true,
  enableTracing: true
};

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

PagesCompnent只是一个简单的router-outlet

PagesModule (我之前显示的那个):

const routes: Routes = [{
  path: '',
  component: PagesComponent,
  children: [
    {
      path: 'dashboard',
      loadChildren: 'app/pages/dashboard/dashboard.module#DashboardModule'
    },
    {
       path: 'members',
       loadChildren: 'app/pages/members/members.module#MembersModule'
     },
    { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: '**', redirectTo: 'dashboard', pathMatch: 'full' },
  ],
}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class PagesRoutingModule {
}

DashboardRoutingModule:

const routes: Routes = [{
  path: '',
  component: DashboardComponent
}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class DashboardRoutingModule {
}

DashboardComponent是模板

MembersRoutingModule:

const routes: Routes = [{
    path: '',
    component: MembersComponent,
    children: [
      {
        path: 'add',
        loadChildren: 'app/pages/members/add/members-add.module#MembersAddModule'
      },
      { path: '', redirectTo: 'add', pathMatch: 'full' },
      { path: '**', redirectTo: 'add', pathMatch: 'full' },
    ],
}];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class MembersRoutingModule {
}

MembersComponent这是一个简单的router-outlet

MembersAddModule:

@NgModule({
  imports: [
    MembersAddRoutingModule,
    ThemeModule,
  ],
  declarations: [
    MembersAddComponent,
  ],
})
export class MembersAddModule { }

我尝试过这种方式:

const routes: Routes = [{
  path: '',
  component: PagesComponent,
  children: [
    {
       path: 'members',
       loadChildren: 'app/pages/members/members.module#MembersModule'
    },
    {
      path: 'dashboard',
      loadChildren: 'app/pages/dashboard/dashboard.module#DashboardModule'
    },
    { path: '', redirectTo: 'members', pathMatch: 'full' },
    { path: '**', redirectTo: 'members', pathMatch: 'full' },
  ],
}];

,但作用相同...

2 个答案:

答案 0 :(得分:0)

from kubernetes.stream import stream
#response = cli.connect_post_namespaced_pod_exec(pod, ns, stderr=True, stdin=True,stdout=True, command=cmd)
response = stream(cli.connect_post_namespaced_pod_exec,pod, ns, stderr=True,stdin=True, stdout=True, command=cmd)

您的基本路线(默认和错误)将重定向到仪表板,这意味着无论您执行什么操作,要加载的第一页都是仪表板。

在会员页面上尝试重新加载,然后导航,看看会发生什么。

答案 1 :(得分:0)

已解决: 我正在将DashboardModule导入PagesModule中。 现在删除,一切正常。