我正在尝试从JSON文件加载路由,路由中也有一个延迟加载的模块,一切都按预期工作,直到代码以aot模式在ng build --prod中运行。当我尝试转到延迟加载的模块链接时,出现以下错误。代码如下:
app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';
import * as AppRoutingJson from '../assets/data/routing.json';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { RouteoneComponent } from './routeone/routeone.component';
import { RoutetwoComponent } from './routetwo/routetwo.component';
const routes: Routes =[];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
entryComponents: [PageNotFoundComponent, RouteoneComponent, RoutetwoComponent],
})
export class AppRoutingModule {
constructor(private router: Router) {
this.prepareRoutes(AppRoutingJson);
}
prepareRoutes(routesJson: any) {
let routesArr = [] as Routes;
routesArr = [
{
path: 'routeone',
component: RouteoneComponent,
},
{
path: 'routetwo',
component: RoutetwoComponent,
},
{ path: 'contact', loadChildren: () => import('./contact/contact.module').then(m => m.ContactModule)},
{ path: 'dv', loadChildren: './modules/dv.module#DVModule' },
];
// routesArr=AppRoutingJson;
routesArr.forEach(route => {
routes.push(route);
});
routes.push(
{
path: 'page-not-found',
component: PageNotFoundComponent,
},
{
path: '**',
redirectTo: 'page-not-found',
}
);
console.log(routes);
this.router.resetConfig(routes);
}
}
错误如下:
答案 0 :(得分:0)
这似乎是一个常见问题。参见此处:https://github.com/angular/angular-cli/issues/10582和此处:https://github.com/angular/angular/issues/23878
我建议对contact
路由使用字符串语法,因此:loadChildren: ./contact/contact.module#ContactModule
。这似乎可以解决那些GitHub问题中的其他问题。