角2包解决不了的延迟加载模块

时间:2019-01-30 09:44:27

标签: angular npm node-modules

我准备了angular 2模块(假设它是Core模块),该模块发布在npm上。此Core模块在Angular项目中作为主模块运行(在main.ts中作为bootstrap模块导入)。

但问题是,如果我建立包括从项目文件夹模块,并运行NG与--aot服务,一切工作良好。但是,如果我从dist或node_modules作为构建包导入它,则当我使用--aot运行它时,延迟加载的组件将停止工作。

文件的

类比是:

AppRouterModule 中设置了 DefaultModule

的路由
import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';
import {AuthGuard} from './services/auth-guard.service';

const appRoutes: Routes = [
  {path: '', redirectTo: 'default', pathMatch: 'full'},
  {path: 'default', loadChildren: './default/default.module#DefaultModule'}
];

@NgModule({
  imports: [
    RouterModule.forRoot(appRoutes)
  ],
  exports: [
    RouterModule
  ],
  providers: [
    AuthGuard
  ]
})
export class AppRoutingModule {

}

核心模块被导入的 DefaultModule

@NgModule({
  imports: [
    DefaultModule,
  ],
  declarations: [
    ...
  ],
  providers: [
    ...
  ],
  bootstrap: [
    ...
  ]
})

DefaultModule 中通过 DefaultRoutingModule

设置了组件和子路由
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DefaultComponent } from './default.component';
import { DefaultRoutingModule } from './default-routing.module';
import { SharedModule } from '../shared/shared.module';
import { ReactiveFormsModule } from '@angular/forms';
import {NgxPermissionsModule} from 'ngx-permissions';

@NgModule({
  imports: [
    CommonModule,
    DefaultRoutingModule,
    SharedModule,
    ReactiveFormsModule,
    NgxPermissionsModule.forChild(),
  ],
  declarations: [
    DefaultComponent,
  ],
  entryComponents: [
  ],
})
export class DefaultModule { }

DefaultRoutingModule

import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {NgxPermissionsGuard} from 'ngx-permissions';
import {DefaultComponent} from './default.component';
import {AuthGuard} from '../services/auth-guard.service';

const defaultRoutes: Routes = [
  {
    path: '', component: DefaultComponent, children: [
      {
        path: '', component: DefaultComponent, canActivate: [NgxPermissionsGuard, AuthGuard],
        data: {
          permissions: {
            only: ['default'],
            redirectTo: ''
          }
        }
      },
    ]
  },
];

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

}

从node_modules导入Core后,当ng serve --aot时,显示错误:

ERROR in Could not resolve module ./default/default.module relative to ../node_modules/...core.module.ts

有人能向我解释,在那里是错误?当核心并没有搬进node_modules为模块,一切正常。

0 个答案:

没有答案