Angular 6路由重定向

Anu*_*TBE 3 angular-routing angular

我是新手Angular并编写了第一个Angular管理仪表板应用程序.

我必须设置两个布局

  1. 用于身份验证,如登录,注销,忘记密码等
  2. 管理员仪表板将在登录后到来

为此我设置了两个布局组件

  1. 管理布局
  2. AUTH-布局

并将所有auth组件放入auth模块中

我的应用程序的目录结构是这样的

app
|- e2e
|- node_modules
|- src
   |- app
      |- auth (module)
         |- login (component)
            |- login.component.ts
            |- login.component.html
         |- register (component)
            |- register.component.ts
            |- register.component.html
         |- auth.module.ts
      |- dashboard (component for auth users)
         |- dashboard.component.ts
         |- dashboard.component.html
      |- layouts ( directory)
         |- admin-layout (component)
            |- admin-layout.component.html
            |- admin-layout.component.ts
            |- admin-layout.module.ts
            |- admin-layout.routing.ts
         |- auth-layout (component)
            |- auth-layout.component.html
            |- auth-layout.component.ts
            |- auth-layout.module.ts
            |- auth-layout.routing.ts
      |- app.component.html
      |- app.component.ts
      |- app.module.ts
      |- app-routing.module.ts
   |- assets
   |- index.html
Run Code Online (Sandbox Code Playgroud)

app-routing.module.ts的内容

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

import {AppRoutingModule} from './app-routing.module';
import { AppComponent } from './app.component';
import {FormsModule} from '@angular/forms';
import {RouterModule} from '@angular/router';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {HttpClientModule} from '@angular/common/http';
import { AdminLayoutComponent } from './layouts/admin-layout/admin-layout.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {ComponentsModule} from './components/components.module';
import { AuthLayoutComponent } from './layouts/auth-layout/auth-layout.component';

@NgModule({
  declarations: [
    AppComponent,
    AdminLayoutComponent,
    AuthLayoutComponent
  ],
  imports: [
    BrowserAnimationsModule,
    FormsModule,
    HttpClientModule,
    RouterModule,
    AppRoutingModule,
    NgbModule.forRoot(),
    ComponentsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

app-routing.module.ts的内容

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

import {AdminLayoutComponent} from './layouts/admin-layout/admin-layout.component';
import {CommonModule} from '@angular/common';
import {BrowserModule} from '@angular/platform-browser';
import {AuthLayoutComponent} from './layouts/auth-layout/auth-layout.component';

const routes: Routes = [
  { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
  { path: '', component: AdminLayoutComponent, children: [
      {path: '', loadChildren: './layouts/admin-layout/admin-layout.module#AdminLayoutModule'}
    ] },
  { path: '', component: AuthLayoutComponent, children: [
      {path: '', loadChildren: './layouts/auth-layout/auth-layout.module#AuthLayoutModule'}
    ]}
];

@NgModule({
  imports: [
    CommonModule,
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

admin-layout.routing.ts的内容

import { Routes } from '@angular/router';
import {DashboardComponent} from '../../dashboard/dashboard.component';

export const AdminLayoutRoutes: Routes = [
  { path: 'dashboard', component: DashboardComponent}
];
Run Code Online (Sandbox Code Playgroud)

admin-layout.module.ts的内容

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { AdminLayoutRoutes } from './admin-layout.routing';
import { DashboardComponent } from '../../dashboard/dashboard.component';
import { IconsComponent } from '../../icons/icons.component';
import {ChartsModule} from 'ng2-charts';

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(AdminLayoutRoutes),
    FormsModule,
    ChartsModule,
    NgbModule
  ],
  declarations: [
    DashboardComponent,
    IconsComponent
  ]
})
export class AdminLayoutModule { }
Run Code Online (Sandbox Code Playgroud)

auth-layout.module.ts的内容

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {RouterModule} from '@angular/router';
import {AuthLayoutRoutes} from './auth-layout.routing';
import {FormsModule} from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {AuthModule} from '../../auth/auth.module';
import {LoginComponent} from '../../auth/login/login.component';
import {RegisterComponent} from '../../auth/register/register.component';

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(AuthLayoutRoutes),
    FormsModule,
    NgbModule,
    AuthModule
  ],
  declarations: [
    LoginComponent,
    RegisterComponent
  ]
})
export class AuthLayoutModule { }
Run Code Online (Sandbox Code Playgroud)

auth-layout.routing.ts的内容

import {Routes} from '@angular/router';
import {LoginComponent} from '../../auth/login/login.component';
import {RegisterComponent} from '../../auth/register/register.component';
import {ForgotPasswordComponent} from '../../auth/forgot-password/forgot-password.component';
import {ResetPasswordComponent} from '../../auth/reset-password/reset-password.component';

export const AuthLayoutRoutes: Routes = [
  {path: 'login', component: LoginComponent},
  {path: 'register', component: RegisterComponent},
  {path: 'forgot-password', component: ForgotPasswordComponent},
  {path: 'reset-password', component: ResetPasswordComponent}
];
Run Code Online (Sandbox Code Playgroud)

在访问时localhost:4200重定向到,localhost:4200/dashboard并且它正在dashboard管理路由中设置路径.

但是在访问时localhost:4200/login重定向到,localhost:4200并且登录路由无法通过身份验证路由.

应用代码网址:https://stackblitz.com/edit/angular-lwevqj
应用网址:https://angular-lwevqj.stackblitz.io

edu*_*eth 8

你的路线应该像 -

const routes: Routes = [
  { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
  { path: 'admin', component: AdminLayoutComponent, children: [
      {path: '', loadChildren: './layouts/admin-layout/admin-layout.module#AdminLayoutModule'}
    ] },
  { path: 'auth', component: AuthLayoutComponent, children: [
      {path: '', loadChildren: './layouts/auth-layout/auth-layout.module#AuthLayoutModule'}
    ]}
];
Run Code Online (Sandbox Code Playgroud)

您的身份验证路线也应该像 -

export const AuthLayoutRoutes: Routes = [
  {path: '', component: LoginComponent},
  {path: 'register', component: RegisterComponent},
  {path: 'forgot-password', component: ForgotPasswordComponent},
  {path: 'reset-password', component: ResetPasswordComponent}
];
Run Code Online (Sandbox Code Playgroud)

电话应该是 -

登录 - localhost:4200/auth/login 注册 - localhost:4200/auth/register 同样的其他人