ERROR TypeError: Cannot read property 'loadChildren' of undefined
ERROR TypeError: Cannot read property 'loadChildren' of undefined
我刚刚添加了一些模块 + 路由 + AuthGuard 以重定向到那些(登录)页面。
一切似乎都正常(我可以导航到它们,页面之间的链接有效,AuthGuard 正确重定向),但我在 chrome 日志中收到此错误:
core.js:4197 ERROR TypeError: Cannot read property 'loadChildren' of undefined
at RouterPreloader.processRoutes (router.js:5220)
at RouterPreloader.processRoutes (router.js:5222)
at RouterPreloader.preload (router.js:5208)
at MergeMapSubscriber.project (router.js:5203)
at MergeMapSubscriber._tryNext (mergeMap.js:46)
at MergeMapSubscriber._next (mergeMap.js:36)
at MergeMapSubscriber.next (Subscriber.js:49)
at FilterSubscriber._next (filter.js:33)
at FilterSubscriber.next (Subscriber.js:49)
at Subject.next (Subject.js:39)
我正在使用 angular 10.0.14,不确定是什么导致了这个问题?
仅当我在其中一个新页面中时才会出现此问题。
这是我的授权模块:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';
import { AuthRoutingModule } from './auth-routing.module';
import { LostPasswordComponent } from './lost-password/lost-password.component';
import { SignupComponent } from './signup/signup.component';
@NgModule({
declarations: [LoginComponent, SignupComponent, LostPasswordComponent, SignupComponent],
imports: [
CommonModule,
AuthRoutingModule
]
})
export class AuthModule { }
这是 AuthRoutingModule:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { LostPasswordComponent } from './lost-password/lost-password.component';
import { SignupComponent } from './signup/signup.component';
const routes: Routes = [
{
path: '',
redirectTo: 'signin',
pathMatch: 'full',
},
{
path: 'signin',
component: LoginComponent,
},
{
path: 'signup',
component: SignupComponent,
},
,
{
path: 'lost-password',
component: LostPasswordComponent,
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class AuthRoutingModule {}
这些页面只是相互之间有链接的空页面,例如:
<p>login works!</p>
<a [routerLink]="['/auth/signup']" routerLinkActive="router-link-active" >Sign up instead</a>
<a [routerLink]="['/auth/lost-password']" routerLinkActive="router-link-active" >Password lost?</a>
如果我进入其他模块的页面之一,没有错误。
AppRouting 模块:
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './auth/auth.guard';
const routes: Routes = [
{
path: '',
redirectTo: 'folder/Inbox',
pathMatch: 'full',
},
{
path: 'folder/:id',
loadChildren: () => import('./folder/folder.module').then((m) => m.FolderPageModule),
canActivate: [AuthGuard],
},
{
path: 'auth',
loadChildren: () => import('./auth/auth.module').then((m) => m.AuthModule),
},
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule],
})
export class AppRoutingModule {}
知道问题出在哪里吗?或者如何解决?
您可以尝试导出 AuthRoutingModule
:
@NgModule({
declarations: [LoginComponent, SignupComponent, LostPasswordComponent, SignupComponent],
imports: [
CommonModule,
AuthRoutingModule
],
export: [AuthRoutingModule]
})
export class AuthModule { }
实际上错误是您 AuthRoutingModule
中多了一个逗号。删除它应该可以修复此错误。
{
path: 'signup',
component: SignupComponent,
},
,
在我们的例子中,我们只需要添加 aot 选项(我们的项目有点老):
{
...
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"aot": true,
...
我们新添加了一些语法,并且 aot 选项在构建阶段提供帮助。将语法更改为传统方式也可以解决它。
我刚刚添加了一些模块 + 路由 + AuthGuard 以重定向到那些(登录)页面。
一切似乎都正常(我可以导航到它们,页面之间的链接有效,AuthGuard 正确重定向),但我在 chrome 日志中收到此错误:
core.js:4197 ERROR TypeError: Cannot read property 'loadChildren' of undefined
at RouterPreloader.processRoutes (router.js:5220)
at RouterPreloader.processRoutes (router.js:5222)
at RouterPreloader.preload (router.js:5208)
at MergeMapSubscriber.project (router.js:5203)
at MergeMapSubscriber._tryNext (mergeMap.js:46)
at MergeMapSubscriber._next (mergeMap.js:36)
at MergeMapSubscriber.next (Subscriber.js:49)
at FilterSubscriber._next (filter.js:33)
at FilterSubscriber.next (Subscriber.js:49)
at Subject.next (Subject.js:39)
我正在使用 angular 10.0.14,不确定是什么导致了这个问题?
仅当我在其中一个新页面中时才会出现此问题。
这是我的授权模块:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';
import { AuthRoutingModule } from './auth-routing.module';
import { LostPasswordComponent } from './lost-password/lost-password.component';
import { SignupComponent } from './signup/signup.component';
@NgModule({
declarations: [LoginComponent, SignupComponent, LostPasswordComponent, SignupComponent],
imports: [
CommonModule,
AuthRoutingModule
]
})
export class AuthModule { }
这是 AuthRoutingModule:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { LostPasswordComponent } from './lost-password/lost-password.component';
import { SignupComponent } from './signup/signup.component';
const routes: Routes = [
{
path: '',
redirectTo: 'signin',
pathMatch: 'full',
},
{
path: 'signin',
component: LoginComponent,
},
{
path: 'signup',
component: SignupComponent,
},
,
{
path: 'lost-password',
component: LostPasswordComponent,
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class AuthRoutingModule {}
这些页面只是相互之间有链接的空页面,例如:
<p>login works!</p>
<a [routerLink]="['/auth/signup']" routerLinkActive="router-link-active" >Sign up instead</a>
<a [routerLink]="['/auth/lost-password']" routerLinkActive="router-link-active" >Password lost?</a>
如果我进入其他模块的页面之一,没有错误。
AppRouting 模块:
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './auth/auth.guard';
const routes: Routes = [
{
path: '',
redirectTo: 'folder/Inbox',
pathMatch: 'full',
},
{
path: 'folder/:id',
loadChildren: () => import('./folder/folder.module').then((m) => m.FolderPageModule),
canActivate: [AuthGuard],
},
{
path: 'auth',
loadChildren: () => import('./auth/auth.module').then((m) => m.AuthModule),
},
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule],
})
export class AppRoutingModule {}
知道问题出在哪里吗?或者如何解决?
您可以尝试导出 AuthRoutingModule
:
@NgModule({
declarations: [LoginComponent, SignupComponent, LostPasswordComponent, SignupComponent],
imports: [
CommonModule,
AuthRoutingModule
],
export: [AuthRoutingModule]
})
export class AuthModule { }
实际上错误是您 AuthRoutingModule
中多了一个逗号。删除它应该可以修复此错误。
{
path: 'signup',
component: SignupComponent,
},
,
在我们的例子中,我们只需要添加 aot 选项(我们的项目有点老):
{
...
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"aot": true,
...
我们新添加了一些语法,并且 aot 选项在构建阶段提供帮助。将语法更改为传统方式也可以解决它。