EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/home/home.module'
EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/home/home.module'
我正在尝试使用路由器延迟加载 Angular 2 个模块,但出现此错误:
error_handler.js:50 EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/home/home.module'
我尝试了所有似乎对其他人都有效的答案,比如这个似乎是每个面临这个问题的解决方案,但对我不起作用
这是我的代码:app.module
import { MediatorService } from './home/mediator.service';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import appRoutes from "./app.routes";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
appRoutes
],
providers: [MediatorService],
bootstrap: [AppComponent]
})
export class AppModule { }
app.routes
import { RouterModule } from '@angular/router';
const routes = [
{path : '', loadChildren: './home/home.module#HomeModule'},
{path: 'devis', loadChildren: './forms/forms.module#FormsModule'}
];
export default RouterModule.forRoot(routes);
home.module
import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import homeRoutes from "./home.routes";
@NgModule({
imports:[CommonModule, homeRoutes],
declarations: [HomeComponent]
})
export default class HomeModule{}
home.routes
import {RouterModule} from "@angular/router";
import {HomeComponent} from "./home.component";
const routes = [
{path: '', component: HomeComponent}
];
export default RouterModule.forChild(routes);
Package.json
{
"name": "insurance",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"@angular/common": "^2.3.1",
"@angular/compiler": "^2.3.1",
"@angular/core": "^2.3.1",
"@angular/forms": "^2.3.1",
"@angular/http": "^2.3.1",
"@angular/platform-browser": "^2.3.1",
"@angular/platform-browser-dynamic": "^2.3.1",
"@angular/router": "^3.3.1",
"bootstrap": "^4.0.0-alpha.5",
"core-js": "^2.4.1",
"font-awesome": "^4.7.0",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@angular/compiler-cli": "^2.3.1",
"@types/jasmine": "2.5.38",
"@types/jquery": "^2.0.34",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.24",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.0.2",
"typescript": "~2.0.3"
}
}
更新
我设法让它在 plunker 上运行
https://plnkr.co/edit/uLxmxDIeCdDzxbFjYQS7?p=preview
但我的机器上仍然没有任何内容!!!!
更新
我安装了一个新的虚拟机ubuntu 16.04
我有同样的问题!可能是因为模块的版本,我的意思是 package.json 上的那些?我如何找出 plunker 中使用的版本,因为它在那里工作。
我设法让它工作,这是我所做的:
1 - 在模块中创建路由代码(不是文件)
2 - 在组件的父目录中制作模块文件
3 - 像这样删除导出中的 'default'
export DEFAULT class HomeModule { }
变成了
export class HomeModule { }
您可以在此处看到它适用于 beta 24:
https://github.com/mauricedb/lazy-routes
我不知道发生了什么!!!
当您使用 export default class SomeModule { }
时,angular-cli 渲染器似乎在延迟加载方面存在问题...以及其他一些细微差别。
这就是我为解决相同问题所做的工作 "Error: Cannot find module..." 我正在部署 Heroku:
- 从应用程序根目录获取所有 loadChildren 路径并包含模块名称的散列
loadChildren: 'app/main/some-module/some-module.module#SomeModule'
- 将
export default class SomeModule { }
更改为export class SomeModule { }
我发现这个问题的症状和上下文非常相似,所以说 帮助我解决问题似乎很有用。
在我的具体情况下,我有点遵循 the lazy feature modules docs, and I even faithfully tried to replicate the associated StackBlitz example 代码。出于某种原因,这个例子逃脱了:
loadChildren: 'app/customers/customers.module#CustomersModule'
即使我的 Angular 基于 CLI (v6) 的实验具有类似的文件夹结构,我也需要这样做:
// Full path including `src` at the start:
loadChildren: 'src/app/customers/customers.module#CustomersModule'
或者这个:
// Relative path from the `app-routing.module.ts` file:
loadChildren: './customers/customers.module#CustomersModule'
不知道为什么 StackBlitz 示例与第一个代码示例脱节,但其他两个都有意义并且在执行 ng serve
时对我有用。
Angular 命令行界面:6.1.5
节点:8.11.3
OS: win32 x64
Angular: 6.1.6
听起来很傻,在 Angular 6.
我在开发我的应用程序时使用了这个命令 ng build --aot --watch
。
不知何故进入该区域我保存了很多文件(从其他一些项目复制粘贴)。构建确实有效,但未显示错误,但浏览器显示此错误。
我关闭了构建,并再次重建它,所有未显示的错误(与上述无关)都出现了!!
还要注意文件 'app-routing.module.ts' 中的以下内容,即
常量路线:路线= [
{path:'login',loadChildren:'./login/login.module#LoginModule'},
{path:'registration',loadChildren:'./registration/registration.module#RegistrationModule'}
];
上面粗体字要大写
对我来说,我必须使用 Module Map NgFactory Loader 你可以这样做:
npm install @nguniversal/module-map-ngfactory-loader --save
然后将 module-map-ngfactory-loader 添加到您的服务器模块:
import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader';
@NgModule({
imports: [
AppModule,
ServerModule,
ModuleMapLoaderModule
],
bootstrap: [AppComponent],
})
export class AppServerModule {}
我所需要的只是重新启动服务器,我猜这与捆绑有关。
npm start
或
ng serve
查看另一个描述相同的 Whosebug 。
您需要更改您的应用程序-routing.module.ts
import { RouterModule } from '@angular/router';
const routes = [
{path : '', loadChildren: () => import('./home/home.module').then(m => m.HomeModule) },
{path: 'devis', loadChildren: () => import('./forms/forms.module').then(m => m.FormsModule) }
];
这样使用
{
path : 'company',
loadChildren: () => import('./company/company.module').then(m => m.CompanyModule)
},
对于 Angular 8 和 9,延迟加载声明已更改。
由于Angular8引入了新的推荐模块加载方式,之前懒加载模块的默认方式是指定模块的字符串路径:
{ path: 'auth', loadChildren: 'src/app/auth/auth.module#AuthModule' }
模块导入方式改为动态导入。动态导入是基于 promise 的,让您可以访问模块,可以在其中调用模块的 class。因此,您的导入现在应该更改为:
{ path: 'auth', loadChildren: () => import('src/app/auth/auth.module').then(m => m.AuthModule) }
我遇到了问题,虽然它就像这里的答案 并通过在 home.module
文件中添加 providers: []
解决了它。
我正在尝试使用路由器延迟加载 Angular 2 个模块,但出现此错误:
error_handler.js:50 EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/home/home.module'
我尝试了所有似乎对其他人都有效的答案,比如这个似乎是每个面临这个问题的解决方案,但对我不起作用
这是我的代码:app.module
import { MediatorService } from './home/mediator.service';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import appRoutes from "./app.routes";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
appRoutes
],
providers: [MediatorService],
bootstrap: [AppComponent]
})
export class AppModule { }
app.routes
import { RouterModule } from '@angular/router';
const routes = [
{path : '', loadChildren: './home/home.module#HomeModule'},
{path: 'devis', loadChildren: './forms/forms.module#FormsModule'}
];
export default RouterModule.forRoot(routes);
home.module
import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import homeRoutes from "./home.routes";
@NgModule({
imports:[CommonModule, homeRoutes],
declarations: [HomeComponent]
})
export default class HomeModule{}
home.routes
import {RouterModule} from "@angular/router";
import {HomeComponent} from "./home.component";
const routes = [
{path: '', component: HomeComponent}
];
export default RouterModule.forChild(routes);
Package.json
{
"name": "insurance",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update --standalone false --gecko false",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"@angular/common": "^2.3.1",
"@angular/compiler": "^2.3.1",
"@angular/core": "^2.3.1",
"@angular/forms": "^2.3.1",
"@angular/http": "^2.3.1",
"@angular/platform-browser": "^2.3.1",
"@angular/platform-browser-dynamic": "^2.3.1",
"@angular/router": "^3.3.1",
"bootstrap": "^4.0.0-alpha.5",
"core-js": "^2.4.1",
"font-awesome": "^4.7.0",
"rxjs": "^5.0.1",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@angular/compiler-cli": "^2.3.1",
"@types/jasmine": "2.5.38",
"@types/jquery": "^2.0.34",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.24",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.0.2",
"typescript": "~2.0.3"
}
}
更新
我设法让它在 plunker 上运行
https://plnkr.co/edit/uLxmxDIeCdDzxbFjYQS7?p=preview
但我的机器上仍然没有任何内容!!!!
更新
我安装了一个新的虚拟机ubuntu 16.04 我有同样的问题!可能是因为模块的版本,我的意思是 package.json 上的那些?我如何找出 plunker 中使用的版本,因为它在那里工作。
我设法让它工作,这是我所做的:
1 - 在模块中创建路由代码(不是文件)
2 - 在组件的父目录中制作模块文件
3 - 像这样删除导出中的 'default'
export DEFAULT class HomeModule { }
变成了
export class HomeModule { }
您可以在此处看到它适用于 beta 24: https://github.com/mauricedb/lazy-routes
我不知道发生了什么!!!
当您使用 export default class SomeModule { }
时,angular-cli 渲染器似乎在延迟加载方面存在问题...以及其他一些细微差别。
这就是我为解决相同问题所做的工作 "Error: Cannot find module..." 我正在部署 Heroku:
- 从应用程序根目录获取所有 loadChildren 路径并包含模块名称的散列
loadChildren: 'app/main/some-module/some-module.module#SomeModule'
- 将
export default class SomeModule { }
更改为export class SomeModule { }
我发现这个问题的症状和上下文非常相似,所以说
在我的具体情况下,我有点遵循 the lazy feature modules docs, and I even faithfully tried to replicate the associated StackBlitz example 代码。出于某种原因,这个例子逃脱了:
loadChildren: 'app/customers/customers.module#CustomersModule'
即使我的 Angular 基于 CLI (v6) 的实验具有类似的文件夹结构,我也需要这样做:
// Full path including `src` at the start:
loadChildren: 'src/app/customers/customers.module#CustomersModule'
或者这个:
// Relative path from the `app-routing.module.ts` file:
loadChildren: './customers/customers.module#CustomersModule'
不知道为什么 StackBlitz 示例与第一个代码示例脱节,但其他两个都有意义并且在执行 ng serve
时对我有用。
Angular 命令行界面:6.1.5 节点:8.11.3 OS: win32 x64 Angular: 6.1.6
听起来很傻,在 Angular 6.
我在开发我的应用程序时使用了这个命令 ng build --aot --watch
。
不知何故进入该区域我保存了很多文件(从其他一些项目复制粘贴)。构建确实有效,但未显示错误,但浏览器显示此错误。
我关闭了构建,并再次重建它,所有未显示的错误(与上述无关)都出现了!!
还要注意文件 'app-routing.module.ts' 中的以下内容,即
常量路线:路线= [ {path:'login',loadChildren:'./login/login.module#LoginModule'}, {path:'registration',loadChildren:'./registration/registration.module#RegistrationModule'} ];
上面粗体字要大写
对我来说,我必须使用 Module Map NgFactory Loader 你可以这样做:
npm install @nguniversal/module-map-ngfactory-loader --save
然后将 module-map-ngfactory-loader 添加到您的服务器模块:
import {ModuleMapLoaderModule} from '@nguniversal/module-map-ngfactory-loader';
@NgModule({
imports: [
AppModule,
ServerModule,
ModuleMapLoaderModule
],
bootstrap: [AppComponent],
})
export class AppServerModule {}
我所需要的只是重新启动服务器,我猜这与捆绑有关。
npm start
或
ng serve
查看另一个描述相同的 Whosebug
您需要更改您的应用程序-routing.module.ts
import { RouterModule } from '@angular/router';
const routes = [
{path : '', loadChildren: () => import('./home/home.module').then(m => m.HomeModule) },
{path: 'devis', loadChildren: () => import('./forms/forms.module').then(m => m.FormsModule) }
];
这样使用
{
path : 'company',
loadChildren: () => import('./company/company.module').then(m => m.CompanyModule)
},
对于 Angular 8 和 9,延迟加载声明已更改。 由于Angular8引入了新的推荐模块加载方式,之前懒加载模块的默认方式是指定模块的字符串路径:
{ path: 'auth', loadChildren: 'src/app/auth/auth.module#AuthModule' }
模块导入方式改为动态导入。动态导入是基于 promise 的,让您可以访问模块,可以在其中调用模块的 class。因此,您的导入现在应该更改为:
{ path: 'auth', loadChildren: () => import('src/app/auth/auth.module').then(m => m.AuthModule) }
我遇到了问题,虽然它就像这里的答案 home.module
文件中添加 providers: []
解决了它。