Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将路由保护应用于功能模块路由模块?_Javascript_Angular_Routing_Canactivate_Angular Route Guards - Fatal编程技术网

Javascript 如何将路由保护应用于功能模块路由模块?

Javascript 如何将路由保护应用于功能模块路由模块?,javascript,angular,routing,canactivate,angular-route-guards,Javascript,Angular,Routing,Canactivate,Angular Route Guards,所以,我正在构建一个Angular 4应用程序,我希望所有路由都得到保护(当然除了登录路由)。我正在尝试使用功能模块和功能模块路由。那么,想象一下这样的情况: import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { AuthGuardService } from './auth/auth-guard.service'; import { L

所以,我正在构建一个Angular 4应用程序,我希望所有路由都得到保护(当然除了登录路由)。我正在尝试使用功能模块和功能模块路由。那么,想象一下这样的情况:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuardService } from './auth/auth-guard.service';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { ProtectedRouteComponent } from './protected-route/protected-route.component';

const routes: Routes = [
  { path: 'login', component: LoginComponent }, // no auth guard for login
  {
    path: '',
    canActivate: [AuthGuardService],
    children: [
      {
        path: '',
        children: [
          { path: 'register', component: RegisterComponent },
          { path: 'protected', component: ProtectedRouteComponent }
        ]
      }
    ]
  }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}
,然后是用户功能模块路由配置,如:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { UsersComponent } from './users.component';

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

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class UsersRoutingModule {}
最好的方法是什么:

  • 继续使用功能模块布线,以及
  • 将路由防护应用于用户功能模块路由,而不复制canActivate防护?是否有任何方法可以将其从根路由模块传递给用户功能模块路由
谢谢,

Iraklis正在寻找相同的解决方案,我找到的解决方案包括以下步骤:

  • 不要通过应用程序模块导入功能模块
  • 而是将功能模块作为应用程序模块路由的一部分导入
有一件事需要考虑:

  • 其他模块将无法访问您的UserModule的组件和服务(直到您访问了其中一个
    /users
    路由)
  • 这可能不是问题,因为您的模块可能已经很好地分开了

这一问题是否已得到解答?如果你想重用你的防护,利用一个共享模块在你的应用程序中可用,我确实可以这样做。然而,我的问题更多的是创建一个配置,允许我的防护被“继承”并应用到功能模块中配置的路由。实现这一目标的最佳实践方式是什么?嗯。。你完全可以用延迟加载的模块来实现这一点。我想没有办法做到你想要的。您可以使用服务使方法保持相同,但这有点不规范。
{
    path: '',
    canActivate: [AuthGuardService],
    children: [
      {
        path: '',
        children: [
          { path: 'register', component: RegisterComponent },
          { path: 'protected', component: ProtectedRouteComponent }
        ]
      },
      {
        path: '',
        children: () => UsersModule
      }
    ]
  }