如何为角形路由模块的每个路由设置“基本”路由

时间:2019-04-03 15:31:48

标签: angular angular-router

我有一个auth模块,其中包含几个组件:loginsignuplogout

我在路由模块中声明所有路由:

import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { LoginComponent } from "./login/login.component";
import { SignupComponent } from "./signup/signup.component";
import { LogoutComponent } from "./logout/logout.component";

const routes: Routes = [
  {
      path: "auth/login",
      component: LoginComponent
  },
  {
      path: "auth/signup",
      component: SignupComponent
  },
  {
      path: "auth/logout",
      component: LogoutComponent
  },
];

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

但是我觉得所有这些“身份验证”都是多余的。有没有办法(甚至通过根应用程序路由模块)来指示此路由模块的所有规则之前都具有此auth

编辑 这是我的主模块路由(至少尝试一下):

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

export const routes: Routes = [
    { path: '', redirectTo:'/beers', pathMatch:'full' },
    { path: 'beers', loadChildren: './beers/beers.module#BeersModule'},
    { path: 'auth', loadChildren: './auth/auth.module#AuthModule'},
];

export const AppRoutes: ModuleWithProviders = RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled', enableTracing: true});

1 个答案:

答案 0 :(得分:0)

这绝对有可能,如果没有完整的代码,很难建议您如何具体实现此目的,但是您将在app-routing.module

中执行以下操作
[
      {
        path: '',
        component: App.AppComponent,
        children: [
          { path: 'auth', loadChildren: './auth/auth.module#AuthModule' }
        ]
      }
    ]

然后您的auth.module中的路线如下:

const routes: Routes = [
  {
      path: "login",
      component: LoginComponent
  },
  {
      path: "signup",
      component: SignupComponent
  },
  {
      path: "logout",
      component: LogoutComponent
  },
];