为什么lazyload在子模块上不起作用?

时间:2019-06-18 06:16:04

标签: angular-ui-router

我有一个小问题,如果尝试将loadChildren放在子模块上,我会为延迟加载Angular CLI 8定制路由器,但我会遇到一些错误。 app.routing.ts

import { CommonModule, } from '@angular/common';
import { BrowserModule  } from '@angular/platform-browser';
import { Routes, RouterModule } from '@angular/router';

import { LoginComponent } from './login/login.component';
import {CoreComponent} from './core/core.component';
const routes: Routes = [
  {
    path: '',
    redirectTo: 'dashboard',
    pathMatch: 'full',
  },
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: '',
    component: CoreComponent,
    children: [
      {
        path: '',
        loadChildren: () => import('./core/core.module').then(mod => mod.CoreModule)
      }
    ]
  }
];

@NgModule({
  imports: [
    CommonModule,
    BrowserModule,
    RouterModule.forRoot(routes , {
      enableTracing: false
    })
  ],
  exports: [
    RouterModule
  ],
})
export class AppRoutingModule { }

在core.module.ts->上工作

import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { CoreRoutes } from '../_config/route';
import { TableListComponent } from '../table-list/table-list.component';
import { TypographyComponent } from '../typography/typography.component';
import { IconsComponent } from '../icons/icons.component';
import { MapsComponent } from '../maps/maps.component';
import { NotificationsComponent } from '../notifications/notifications.component';
import { UpgradeComponent } from '../upgrade/upgrade.component';

import {UsersComponent} from '../users/user-list/users.component';
import {UserDetailsComponent} from '../users/user-details/user-details.component';

@NgModule({
    imports: [
        CommonModule,
        RouterModule.forChild(CoreRoutes)
    ],
    declarations: [
        TableListComponent,
        TypographyComponent,
        IconsComponent,
        MapsComponent,
        NotificationsComponent,
        UpgradeComponent,
        UsersComponent,
        UserDetailsComponent
    ],
    providers : [
    ]
})

export class CoreModule {}

在route.ts上->在这种情况下,loadchildren仅在我包含这样的完整路径时才有效'../ dashboard / dashboard.module#DashboardModule'

import { AuthGuard } from '../_helpers';
import {SandboxComponent, UsersComponent} from '../users/user-list/users.component';
import {UserDetailsComponent} from '../users/user-details/user-details.component';
export const CoreRoutes: Routes = [
    {
        path: 'dashboard',
        loadChildren: () => import('../dashboard/dashboard.module').then(mod => mod.DashboardModule),
        // '../dashboard/dashboard.module#DashboardModule'
        canActivate: [AuthGuard],
    },
    {
        path: 'users',
        component: UsersComponent,
        children: [
            { path: 'sandbox', component: SandboxComponent } // url: about/item
        ]
    },
    {
        path: 'users/sandbox',
        component: UsersComponent
    },
    { path: 'user/:id',       component: UserDetailsComponent }
];
ERROR in src\app\core\core.module.ts(18,31): Error during template compile of 'CoreModule'
    Function expressions are not supported in decorators in 'CoreRoutes'
    'CoreRoutes' contains the error at src\app\_config\route.ts(8,23)
    Consider changing the function expression into an exported function.```

1 个答案:

答案 0 :(得分:1)

我发现了这个问题。 问题是,当我使用--aot(Ahead Of Time)参数启动服务器,并且CLI版本为7.X时,如果我像ng serve这样简单地运行,则一切正常(JIT-及时)编译器。 根据那里的文档https://angular.io/guide/aot-compiler,它来自6 CLI版本,将CLI更新到7/8后会发生一些事情