如果路径为空,如何使用 angular 中的模块重定向到登录页面
How to redirect to the login page if the path is null using the modules in angular
我有一个 angular 应用程序,并创建了登录和仪表板页面,我的要求是在重定向时必须转到登录页面,并且仅当路径为仪表板时才必须转到仪表板页面。
我已经为登录创建了 user-pages 文件夹,并在 user-pages 文件夹中创建了模块。
app-routing.module.ts
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'about', component: AboutComponent },
{ path: 'notifications', component: NotificationsComponent },
{ path: '', redirectTo: '/user-pages/login', pathMatch: 'full' },
{ path: 'user-pages', loadChildren: () => import('./user-pages/user-pages.module').then(m => m.UserPagesModule) },
用户-pages.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
]
@NgModule({
declarations: [LoginComponent, RegisterComponent],
imports: [
CommonModule,
RouterModule.forChild(routes)
]
})
export class UserPagesModule { }
我只想在执行时显示登录页面。
谁能帮我解决这个问题
您需要移除空路径重定向
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'about', component: AboutComponent },
{ path: 'notifications', component: NotificationsComponent },
{ path: 'user-pages', loadChildren: () => import('./user-pages/user-pages.module').then(m => m.UserPagesModule) }
];
并像下面这样更改用户页面模块路由
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
]
并在应用组件 ngOnInit 中添加路由
ngOnInit(){
this.router.navigate(['/user-pages']);
}
我有一个 angular 应用程序,并创建了登录和仪表板页面,我的要求是在重定向时必须转到登录页面,并且仅当路径为仪表板时才必须转到仪表板页面。
我已经为登录创建了 user-pages 文件夹,并在 user-pages 文件夹中创建了模块。
app-routing.module.ts
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'about', component: AboutComponent },
{ path: 'notifications', component: NotificationsComponent },
{ path: '', redirectTo: '/user-pages/login', pathMatch: 'full' },
{ path: 'user-pages', loadChildren: () => import('./user-pages/user-pages.module').then(m => m.UserPagesModule) },
用户-pages.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
]
@NgModule({
declarations: [LoginComponent, RegisterComponent],
imports: [
CommonModule,
RouterModule.forChild(routes)
]
})
export class UserPagesModule { }
我只想在执行时显示登录页面。
谁能帮我解决这个问题
您需要移除空路径重定向
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'about', component: AboutComponent },
{ path: 'notifications', component: NotificationsComponent },
{ path: 'user-pages', loadChildren: () => import('./user-pages/user-pages.module').then(m => m.UserPagesModule) }
];
并像下面这样更改用户页面模块路由
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
]
并在应用组件 ngOnInit 中添加路由
ngOnInit(){
this.router.navigate(['/user-pages']);
}