我是新来的
Angular
写我的第一封信
角度
管理仪表板应用程序。
我得设置两个布局
-
用于身份验证,如登录、注销、忘记密码等
-
登录后将出现管理仪表板
为此,我设置了两个布局组件
-
管理布局
-
授权布局
并将所有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
的内容
应用程序路由模块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 { }
的内容
应用程序路由模块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 { }
的内容
管理布局.routing.ts
import { Routes } from '@angular/router';
import {DashboardComponent} from '../../dashboard/dashboard.component';
export const AdminLayoutRoutes: Routes = [
{ path: 'dashboard', component: DashboardComponent}
];
的内容
管理布局.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 { }
的内容
授权布局.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
正在重定向到
本地主机:4200
并且登录路由不能从auth路由工作。
应用程序代码URL:
https://stackblitz.com/edit/angular-lwevqj
应用程序URL:
https://angular-lwevqj.stackblitz.io