无法重定向到延迟加载模块作为服务器启动时的默认路由/url [英] Unable to redirect to Lazy Loaded module as default route/url on server start up
问题描述
大家好,
我的 Angular 2 应用程序在服务器启动时遇到这个问题 (npm start
).
I am having this issue in my Angular 2 application upon server startup (npm start
).
我将基本路由或基本路径重定向到 user/login
,这是一个延迟加载的模块,但它引发了 Cannot find module
错误.正如我所说,这总是发生,在启动和编辑 'full'
到 'prefix'
的 pathMatch
参数期间,反之亦然修复了错误并加载路由而没有错误.
I redirect the base route or base path to user/login
which is a Lazy Loaded module but it throws an Cannot find module
error. This always happens as I've said, during startup and editing the pathMatch
argument from 'full'
to 'prefix'
and vice versa fixes the error and loads the route without error.
希望你能帮我解决这个问题.
I hope you can help me with this.
下面是程序.
app/routes.ts
import { Routes } from '@angular/router';
import { AppComponent } from './app.component';
export const appRoutes:Routes = [
{ path: 'user', loadChildren: 'user/user.module#UserModule'},
{ path: '', redirectTo: 'user/login', pathMatch: 'full'}
]
app/app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { appRoutes } from './routes';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app/user/user.routes.ts
import { LoginComponent } from "./login.component";
export const userRoutes = [
{ path: 'login', component: LoginComponent }
]
app/user/user.module.ts
import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";
import { RouterModule } from "@angular/router";
import { userRoutes } from "./user.routes";
import { LoginComponent } from "./login.component";
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(userRoutes)
],
declarations: [
LoginComponent
],
providers: [UserAuthService]
})
export class UserModule {}
我希望我已经提供了有关我的问题的足够信息.如果没有,请告诉我.谢谢.
I hope I've provided enough information about my problem. If not, just let me know. Thank you.
我在启动时访问 user/login
时遇到同样的错误.
I'm having the same error when I visit user/login
upon startup.
推荐答案
() =>
符号实际上对我有用.下面是关于我如何通过编辑 routes.ts 文件来修复它的代码
The () =>
notation actually worked for me. Below is my code regarding how I fixed it just by editing my routes.ts file
app/routes.ts
import { UserModule } from './user/user.module';
export const appRoutes:Routes = [
{ path: 'user', loadChildren: () => UserModule},
{ path: 'login', component: LoginComponent},
{ path: '', redirectTo: 'user/login', pathMatch: 'full'}
]
这篇关于无法重定向到延迟加载模块作为服务器启动时的默认路由/url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!