默认路由重定向不适用于 Angular 2 中的延迟加载路由

2024-01-21

我有一个应用程序,分为经过身份验证的部分(InternalRootComponent)和匿名部分(ExternalRootComponent)。

当我显式导航到路线时,一切正常,但当我转到根目录 (/) 时,我不会被重定向。此外,由于某种原因加载了帐户组件。

应用程序路由.module.ts:

export const routes: Routes = [
    {
        path: '',
        redirectTo: 'login',
        pathMatch: 'full'
    },
    {
        path: 'login',
        component: ExternalRootComponent,
        children: [
            {
                path: '',
                loadChildren: './login/login.module#LoginModule'
            }
        ]
    },
    {
        path: 'membership',
        component: ExternalRootComponent,
        children: [
            {
                path: '',
                loadChildren: './membership/membership.module#MembershipModule'
            }
        ]
    },
    {
        path: 'app',
        component: InternalRootComponent,
        canActivate: [AuthGuard],
        children: [
            {
                path: '',
                canActivateChild: [AuthGuard],
                children: [
                    {
                        path: '',
                        redirectTo: './dashboard',
                        pathMatch: 'full'
                    },
                    {
                        path: 'dashboard',
                        loadChildren: './dashboard/dashboard.module#DashboardModule'
                    },
                    {
                        path: 'accounts',
                        loadChildren: './accounts/accounts.module#AccountsModule'
                    },
                    {
                        path: 'users',
                        loadChildren: './users/users.module#UsersModule'
                    },
                    {
                        path: 'services',
                        loadChildren: './services/services.module#ServicesModule'
                    },
                    {
                        path: 'support',
                        loadChildren: './support/support.module#SupportModule'
                    }
                ]
            }
        ]
    },
    {
        path: '**',
        component: NotFoundComponent
    }
];

账户路由.module.ts:

const routes: Routes = [
    {
        path: '',
        component: AccountInfoComponent
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class AccountsRoutingModule {}

我不明白的是为什么第一个重定向不起作用 - 我希望 / 重定向到 /login。相反,似乎正在调用accounts-routing.module.ts 中的空路由。


我的猜测是 AccountModule 被导入到根模块中。

这是一个应该有效的通用设置。抱歉,我没有使用您的所有代码,因为我认为用一个最小但完整的示例会更清楚。我对可能导致您所观察到的行为的潜在问题进行了评论。如果没有更多信息,我不能完全确定这会解决您的确切问题,但它至少是相似的,并且应该对某人有帮助。

采用以下使用模块延迟加载的设置:

注意 - 延迟加载可能会导致意外行为,因为 路由器模块导入子路由,特别是如果您将服务捆绑到功能模块中,这需要根级别导入(不过最好将服务分离到它们自己的模块中)。下面的评论应该解释我的意思。

我们的教训是只导入带有路由的惰性模块一次。 (不这样做意味着该模块不能再延迟加载,并且完全违背了延迟加载的目的)如果您有与它们捆绑在一起的服务需要位于根目录中,请将这些服务分离到根目录的不同服务模块中

应用程序模块.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component.ts';
import { routes } from './app-routing.module';

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(routes),
    // I think this might be your issue.
    // DON'T do this (import child module here)
    //
    // MaleChildModule
    // or somethings like this
    // FemaleChildModule.forRoot()
    //
    // NOTE - order doesn't matter either. i.e. putting this on the
    // line above RouterModule.forRoot(routes) will not help
    // 
    // Doing so means the ChildModules and routes are actually being
    // imported twice
    //
    // so these would all be valid paths
    // /female/sally
    // /sally
    // /male/john
    // /john
    //
    // then if you had a module's routes set up like those in 
    // the MaleChildModule the root redirect to /child
    // would not work and it would just be a blank view with no path
    // update in the browser. very confusing situation.
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

应用程序组件.ts

import { Component } from '@angular/core';

@Component({
  selector: 'ex-app',
  template: '<router-outlet></router-outlet>'
})
export class AppComponent {}

应用程序路由.module.ts

import { Routes } from '@angular/router';

export const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'males'
  },
  {
    path: 'males',
    loadChildren: './male-child.module#MaleChildModule'
  },
  {
    path: 'females',
    loadChildren: './female-child.module#FemaleChildModule'
  }
]

注意 - 延迟加载模块导入 RouterModule.forChild(routes) 如果不小心,可能会导致意外行为

男童.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { JohnChildComponent } from './john-child.component.ts';

// NOTE - if you set up your child module like this and make the
// mistake I'm describing (importing child modules multiple times)
// you will get unexpected behavior of redirects not working and
// no clues as to why. I suggest always having empty paths redirect
// to something with a component. FemaleChildModule is an example.
const childRoutes: Routes = [
  {
    path: 'john',
    component: JohnChildComponent
  }
]

@NgModule({
  imports: [
    RouterModule.forChild(childRoutes)
  ],
  declarations: [
    JohnChildComponent
  ]
})
export class MaleChildModule {}

女性儿童.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { SallyChildComponent } from './sally-child.component.ts';

const childRoutes: Routes = [
  {
    path: '',
    children: [
      // NOTE - I like to setup lazy loaded modules like this because
      // it prevents masking of the module loading issue because there
      // are never any paths that don't have an associated component
      {
        path: '',
        pathMatch: 'full',
        redirectTo: 'sally',
      },
      {
        path: 'sally',
        component: SallyChildComponent
      }
   ]
  }
]

@NgModule({
  imports: [
    RouterModule.forChild(childRoutes)
  ],
  declarations: [
    SallyChildComponent
  ]
})
export class FemailChildModule {}

约翰-孩子.component.ts

import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'ex-john',
  template: '<p>john</p>'
})
export class JohnChildComponent {}

sally-child.component.ts

import { Component } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'ex-sally',
  template: '<p>sally</p>'
})
export class SallyChildComponent {}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

默认路由重定向不适用于 Angular 2 中的延迟加载路由 的相关文章

随机推荐

  • 找不到 Alamofire 框架

    我正在尝试将 alamofire 安装到我的项目中 以便我可以将图像上传到我的服务器 但是我似乎找不到alamofire framework文件 我已经下载了两次 git 完成了安装说明https github com Alamofire
  • SQL NOT IN 子句

    我有一个查询未按预期工作 Q1 SELECT id name FROM vw x WHERE id NOT IN select pid from table x GROUP BY id name Having max c date gt G
  • SQL Server:存储过程的 EXECUTE AS 子句未授予 sysadmin 权限

    我开发了一个存储过程 以便从备份文件恢复数据库并向其中添加应用程序用户 该存储过程属于master 数据库 问题是我的 IT 部门不允许我使用管理员用户 只能使用 sysadmin 用户的 EXECUTE AS 语句 我可以恢复数据库 但找
  • 为什么要设置线程的Terminal属性?

    我有多线程应用程序 procedure TGridUpdater Execute begin inherited CodeSite Send Thread executed sp ConnectionFactory GetConnectio
  • 这是使用和测试利用工厂模式的类的正确方法吗?

    我对工厂模式没有太多经验 我遇到过一种情况 我认为这是必要的 但我不确定我是否正确实现了该模式 并且我担心它的影响对我的单元测试的可读性有影响 我创建了一个代码片段 它 根据记忆 近似于我正在工作的场景的本质 如果有人能看一下它并看看我所做
  • 如何隐藏/加密谷歌和其他浏览器中网络选项卡的ajax请求? [复制]

    这个问题在这里已经有答案了 我正在 jwplayer 上工作 我只想隐藏 加密 jwplayer 发送的用于播放视频的 ajax 请求 原因是 当 jwplayer 发送请求时 它包含视频 ID 我不想透露它 因为它是从 google 驱动
  • 使用预填充的核心数据部署应用程序

    我正在尝试将我的应用程序与Core Data已经有人居住 我找到了一些链接 其中解释了如何执行此操作 但要么不起作用 要么答案非常旧 我跟着发帖但不起作用 解决方案可能是导入 sqlite文件到应用程序文件夹 然后将它们复制到设备的文件系统
  • 创建 C# 安装程序的最佳方法

    我使用 Visual C 2008 Express 版 我希望能够为我拥有的项目创建一个安装程序 我可以使用发布工具来完成此操作 但您几乎无法控制 有没有免费的方法可以做到这一点 或者我需要使用 VS2008 的完整版本 或者 我发布时是否
  • 将变量从 [HttpPost] 方法传递到 [HttpGet] 方法

    我将视图从 HttpPost 方法重定向到 HttpGet 方法 我已经让它工作了 但想知道这是否是最好的方法 这是我的代码 HttpPost public ActionResult SubmitStudent StudentViewMod
  • 将灰度滤镜应用于 div

    尝试对主背景上方的 div 应用灰度滤镜 想知道这是否可以通过 jQuery CSS3 或 HTML5 实现 我正在尝试一些新的 CSS3 HTML5 技术 但没有成功 我无法将其保存为两个图像 因为背景需要拉伸全尺寸 因此它在每个屏幕上不
  • itgenobr001:找不到客户端。与 Exact Online 比利时合作的数据接入点

    我们刚刚入住https ecotaksen be https ecotaksen be Exact 上的查询和更新运行良好 但安装生产许可证后出现错误itgenobr001 Client not found occurs 我的数据容器规范是
  • 填充下拉列表客户端。收到回发验证错误

    我有一个带有两个下拉列表的网络控件 当您从第一个下拉列表中选择某些内容时 第二个下拉列表就会相应地填充 当回发发生时 我得到旧的 回发或回调参数无效 事件验证已启用 使用于 配置或 在 页 出于安全目的 这 功能验证参数 回发或回调事件发起
  • JQuery 事件不适用于生产中的 heroku,但适用于开发中

    这似乎是一个常见问题 但我还没有找到适用于我的情况的解决方案 我在 bikes js coffee 中有一些 JQuery 在本地开发中可以正常工作 当我推送到 Heroku 时 bikes js coffee 中的脚本不会运行 浏览器的
  • Boost::几何联合简化 - 它是如何工作的?

    Boost 中有很棒的几何库 它还允许绘制 SVG 图像 我想在我的一些项目中使用它 但它对我来说真的很奇怪 见下图 所以我们有 3 个像素点 在 2d 空间中表示为正方形 1 1 0 1 pic 1 我们想从它们那里得到一个并集并简化它
  • 无法使用 Selenium 的 chrome 驱动程序

    我在使用 Selenium 的 Chrome 驱动程序时遇到问题 我已下载 chromedriver 并将其保存到 C Chrome driver webdriver Chrome executable path C Chrome 使用它会
  • 如何将 X 和 Y 转换为纬度和经度

    我有一张桌子叫IK TEMP它包含名为 data range 的列 String sql SELECT DATA RANGE FROM IK TEMP try Connection conn this connect Statement s
  • 在 Android 上横向模式下隐藏 URL 地址栏

    使用 JavaScript 如何在横向模式下滚动经过 URL 栏 在纵向模式下 您只需执行 window scrollTo 0 1 即可 但在横向模式下则不起作用 它在 URL 栏上部分运行 关于尝试什么有什么建议吗 有两种方法可以做到这一
  • 如何加速简单连接

    我不擅长SQL 我正在寻找一种方法来加速像这样的简单连接 SELECT E expressionID A attributeName A attributeValue FROM attributes A JOIN expressions E
  • 为什么在基于 Identity Cookie 的身份验证中一段时间​​后 User.Identity.IsAuthenticated 设置为 false

    我正在使用 EF core 在 asp net core 2 0 中开发 API 我已经在中配置了身份cookie身份验证startup cs如下 services ConfigureApplicationCookie options gt
  • 默认路由重定向不适用于 Angular 2 中的延迟加载路由

    我有一个应用程序 分为经过身份验证的部分 InternalRootComponent 和匿名部分 ExternalRootComponent 当我显式导航到路线时 一切正常 但当我转到根目录 时 我不会被重定向 此外 由于某种原因加载了帐户