简体   繁体   English

Angular 找不到自定义模块

[英]Angular can't find custom module

I'm trying to convert my Angular 7 app to use modules so that I get lazy loading.我正在尝试将我的 Angular 7 应用程序转换为使用模块,以便延迟加载。 When I run with modules the browser says当我使用模块运行时,浏览器说

core.js:18374 ERROR Error: Uncaught (in promise): Error: Cannot find module './orders/orders.module' core.js:18374 错误错误:未捕获(承诺):错误:找不到模块“./orders/orders.module”
Error: Cannot find module './orders/orders.module'错误:找不到模块“./orders/orders.module”

I'm not sure why that's happening as the .../src/app/orders/orders.module.ts file definitely exists.我不确定为什么会发生这种情况,因为.../src/app/orders/orders.module.ts文件肯定存在。 Originally I just had a .../src/app/orders directory and in it multiple components.最初我只有一个.../src/app/orders目录,其中有多个组件。 To implement the modules I did this为了实现我这样做的模块

% mv src/app/orders orders
% ng g m orders --routing
% mv orders/* src/app/orders

I then removed all the routes from app-routing.module.ts and replaced it with this, so that by default when you go to the app it takes you to the orders list.然后我从app-routing.module.ts删除了所有路由并将其替换为这个,这样默认情况下,当您转到应用程序时,它会将您带到订单列表。

{
    path: 'orders',
    loadChildren: './orders/orders.module#OrdersModule'
},
{
    path: '',
    redirectTo: 'orders',
    pathMatch: 'full'
}

And then orders-routing.module.ts looks like so:然后orders-routing.module.ts看起来像这样:

import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {OrderEditComponent} from './order-edit/order-edit.component';
import {OrderListComponent} from './order-list/order-list.component';

const routes: Routes = [
    {path: 'edit', component: OrderEditComponent},
    {path: 'byMonth', component: OrderListComponent},
    {path: '', component: OrderListComponent},
];

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

orders.module looks like so: orders.module 看起来像这样:

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';

import {OrdersRoutingModule} from './orders-routing.module';
import {OrderListComponent} from './order-list/order-list.component';
import {OrderEditComponent} from './order-edit/order-edit.component';

@NgModule({
    declarations: [
        OrderListComponent,
        OrderEditComponent
    ],
    imports: [
        CommonModule,
        OrdersRoutingModule
    ]
})
export class OrdersModule {
}

You can see the files exist in the proper spot.您可以看到文件存在于正确的位置。

在此处输入图片说明

好的,我建议使用 angular CLI 创建一个新项目,复制 app 文件夹,粘贴到新项目中,然后重新运行它。

在根模块中导入 OrdersModule。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM