Angular 6路由重定向

时间:2018-07-19 06:28:34

标签: angular angular-routing

我是Angular的新手,正在为管理仪表板编写我的第一个Angular应用程序。

我必须设置两个布局

  1. 用于身份验证,例如登录,注销,忘记密码等
  2. 管理控制台将在登录后出现

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

  1. 管理布局
  2. auth-layout

并将所有身份验证组件放入身份验证模块

我的应用程序的目录结构就像

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

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

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

admin-layout.routing.ts

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

export const AdminLayoutRoutes: Routes = [
  { path: 'dashboard', component: DashboardComponent}
];

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

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

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}
];
  

问题

访问localhost:4200时将重定向到localhost:4200/dashboard,并且它的工作方式是在管理路由中设置了dashboard路径。

但是在访问localhost:4200/login时,重定向到localhost:4200,并且登录路由无法从auth路由进行。

  

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

4 个答案:

答案 0 :(得分:7)

您的路线应该像-

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'}
    ]}
];

此外,您的身份验证路由应类似于-

export const AuthLayoutRoutes: Routes = [
  {path: '', component: LoginComponent},
  {path: 'register', component: RegisterComponent},
  {path: 'forgot-password', component: ForgotPasswordComponent},
  {path: 'reset-password', component: ResetPasswordComponent}
];

电话应为-

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

答案 1 :(得分:1)

您应该更改主要路线(用“登录”代替“”),我认为问题出在这里:

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

答案 2 :(得分:0)

您正在使用以下代码 {path:'',redirectTo:'dashboard',pathMatch:'full'} (app-routing.module.ts) 文件,以便在访问 localhost:4200 时重定向到 localhost:4200 / dashboard ,并且可以用作仪表板。

首先默认设置要重定向的 路径位置,例如 {path:、、 redirectTo:'login',pathMatch:'full'}  如果您将使用此代码,则当您输入 localhost:4200 时,您的网址会自动重定向到登录组件。

答案 3 :(得分:0)

它已重定向到/dashboard,因为您的全局路由文件已在顶部定义,以将对Dashboard组件的所有调用重定向。

查看全局路由文件中的第一个path定义

在这个有角度的项目中,层次结构得以建立。假定用户将首先进入仪表板。

如果要直接转到登录组件,只需在全局路由文件中为登录组件路由添加/login

但是请注意登录模块中的路由。我建议仅当您确实想拥有该从属URL时,才使用/child1/child2形式的子路由。

如果要将某些组件设置为/name形式,则它们应该全局存在。

您可以在Angular 6 Docs

上看到更多信息