无法在服务器启动时作为默认路由/URL重定向到“延迟加载"模块 [英] Unable to redirect to Lazy Loaded module as default route/url on server start up

查看:109
本文介绍了无法在服务器启动时作为默认路由/URL重定向到“延迟加载"模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

服务器启动(npm start)时,我的Angular 2应用程序中出现此问题.

I am having this issue in my Angular 2 application upon server startup (npm start).

我将基本路由或基本路径重定向到user/login,这是一个延迟加载"模块,但是会引发Cannot find module错误.正如我已经说过的那样,在启动和编辑pathMatch参数从'full''prefix'时,总是会发生这种情况,反之亦然,这可以修复错误并加载没有错误的路由.

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.

推荐答案

() =>表示法实际上对我有用.以下是有关我如何通过仅编辑route.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆