AngularCli将节点模块导入到角度app.module loadChildren

时间:2018-05-10 10:33:19

标签: angular npm webpack angular-cli angular-cli-v6

我想从节点模块导入我的模块。 此节点模块还具有我需要导航的路径。

我需要的结果:

将我的app.module连接到我的节点模块中的模块中 loadChildren 。 TestModule需要连接到forRoot,所以我可以延迟加载TestModules路由。

TestModule是一个dist文件夹,使用ng-packagr中内置的angular cli版本6添加到节点模块中。

这是我的核心应用模块:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { TestModule } from "test";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
     TestModule,
     BrowserModule,
     RouterModule.forRoot(
      [
        {
          path: "",
          redirectTo: "test",
          pathMatch: "full"
        },
        {
          path: "test",
          loadChildren: "test#TestModule"
        },
     ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是导入的TestModule(这是一个npm包):

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { HomeComponent } from './containers/home/home.component';
import { LayoutSidebarComponent } from './layouts/layout-sidebar/layout-sidebar.component';

const COMPONENTS = [
  HomeComponent
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild([
      {
        path: '',
        pathMatch: 'full',
        component: LayoutSidebarComponent,
        children: [
          {
            path: '',
            component: HomeComponent
          }
        ]
      }
    ])
  ],
  declarations: [...COMPONENTS],
  exports: []
})
export class TestModule {}

这是我的核心应用程序tsconfig.aoo.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "types": [
      "node"
    ]
  },
  "include": [
    "../node_modules/test/**/*.ts"
  ],
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

的信息:

当我执行 ng服务端口localhost:4200 时,我没有收到控制台错误,也没有git命令错误。

我正在使用角度cli 6,角度为6,ng-packagr,webpack和iterm用于git cli。

很难调试,因为我没有错误可以使用。 似乎核心应用程序模块没有加载。

1 个答案:

答案 0 :(得分:1)

AFAIK,当您延迟加载模块时,请勿同时导入它 尝试从app.module.ts

中的导入中删除TestModule。
相关问题