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

查看:13
本文介绍了无法在服务器启动时重定向到延迟加载模块作为默认路由/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 错误.正如我所说,这总是发生,在启动和编辑 '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屋!

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