Angular 如果路径为空,如何使用中的模块重定向到登录页面

Angular 如果路径为空,如何使用中的模块重定向到登录页面,angular,typescript,angular-ui-router,angular-routing,angular-router,Angular,Typescript,Angular Ui Router,Angular Routing,Angular Router,我有一个angular应用程序,创建了登录页面和仪表板页面,我的要求是在重定向时,它必须转到登录页面,只有当路径是仪表板时,它才能转到仪表板页面 我已经创建了用于登录的user pages文件夹,并在user pages文件夹中创建了模块 应用程序路由.module.ts const routes: Routes = [ { path: 'dashboard', component: DashboardComponent }, { path: 'about', component: A

我有一个angular应用程序,创建了登录页面和仪表板页面,我的要求是在重定向时,它必须转到登录页面,只有当路径是仪表板时,它才能转到仪表板页面

我已经创建了用于登录的user pages文件夹,并在user pages文件夹中创建了模块

应用程序路由.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) },
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 { }


用户页面.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) },
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(){
   this.router.navigate(['/user-pages']);
}

解决方案可能是为您要重定向到的路径设置另一个路由。例如{path:'user pages/login',component:LoginComponent}Thanks@fonzane,但我无法理解,请您编辑上面的代码。您也需要共享用户页面。