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

威廉

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

当我显式导航到路由时,一切正常,但是当我访问根(/)时,不会被重定向。另外,由于某种原因,AccountsComponent已加载。

app-routing.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
    }
];

account-routing.module.ts:

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

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

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

白兰度

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

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

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

注意-延迟加载可能会由于路由器模块导入子路由而导致意外行为,尤其是如果您将服务捆绑到功能模块中而需要进行根级导入(虽然最好将服务分离到各自的模块中)。以下评论应解释我的意思。

课程是只导入带有路由的惰性模块一次。(如果不这样做,则模块将不再是惰性的,并且完全无法实现延迟加载的目的)。如果您将捆绑的服务捆绑在根目录中,则需要将这些服务分为不同的根服务模块

app.module.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 {}

app.component.ts

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

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

app-routing.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),如果不小心可能会导致意外行为

male-child.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 {}

女性儿童模块

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 {}

john-child.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 {}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

默认路由不适用于区域路由

在Angular 6中使用延迟加载时,路由不适用于其他路由

其他路由而不是默认路由不适用于XAMPP

Django Viewset 检索不适用于默认路由器

角度路由:延迟加载后,页面将重定向到默认路由

角路由redirectTo不适用于延迟加载

vue-router重定向不适用于路由推送

额外/重定向默认路由时

Angular路由器:命名的插座似乎不适用于相对路由,也不适用于延迟加载的模块

Angular 4路由-所有路由都重定向到默认路由

Angular2 useAsDefault不适用于子路由

嵌套组件不适用于 Angular 中的路由

无法在服务器启动时作为默认路由/ URL重定向到“延迟加载”模块

Angular 的 *ngIf 不适用于重新路由

Angular 2路由器辅助路由不适用于redirectTo

React Router默认路由重定向到/ home

自定义路由不适用于默认 mvc 路由

刷新路由是使用 angular2 路由器定向到默认路由

默认插槽或命名插槽不适用于 Vue 3 中的“路由器视图”

命名路由不适用于url

路由不适用于AngularJS

路由前缀不适用于索引

带有子路由的延迟加载模块中的角路由

延迟加载的路由问题

htaccess 中的重定向不适用于 2 条规则

路由到角度为2+的延迟加载模块中的特定页面

Angular 2+,延迟加载路由失败(无错误)

Angular 2:如何从组件内部读取延迟加载的模块的路由

如何在Angular中测试延迟加载路由的存在?