我有一个奇怪的问题,我克隆了 ngx-admin 并尝试将主题用作基本主题。
我已经启用Layout Components
了Modules
。Routing
创建了路由和连接的组件,但问题是如果我直接从链接打开它们,路由工作正常。但是当我尝试使用router.navigate
重定向时,路由不起作用。
以下是我的文件
应用程序路由.module.ts
import { ExtraOptions, RouterModule, Routes } from '@angular/router';
import { NgModule } from '@angular/core';
import { DashboardLayoutComponent } from './layouts/dashboard-layout/dashboard-layout.component';
import { AuthenticationLayoutComponent } from './layouts/authentication-layout/authentication-layout.component';
const routes = [
{
path: '',
redirectTo: 'app',
pathMatch: 'full',
},
{
path: '',
component: DashboardLayoutComponent,
children: [
{
path: 'app',
loadChildren: () => import('./layouts/dashboard-layout/dashboard-layout.module').then(m => m.DashboardLayoutModule)
}
]
},
{
path: '',
component: AuthenticationLayoutComponent,
children: [
{
path: 'auth',
loadChildren: () => import('./layouts/authentication-layout/authentication-layout.module').then(m => m.AuthenticationLayoutModule)
}
]
},
{
path: '**',
redirectTo: 'app'
}
];
export const AppRoutingModule : Routes = routes;
app.module.ts
@NgModule({
declarations: [
AppComponent,
DashboardLayoutComponent,
AuthenticationLayoutComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
RouterModule.forRoot(AppRoutingModule),
CookieModule.forRoot(),
ThemeModule.forRoot(),
NbSidebarModule.forRoot(),
NbMenuModule.forRoot(),
NbWindowModule.forRoot(),
NbToastrModule.forRoot(),
CoreModule.forRoot(),
FormsModule,
ReactiveFormsModule
],
providers: [AuthGuard, AuthService],
bootstrap: [AppComponent],
})
export class AppModule {
}
auth-layout.routing.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from '../../pages/auth/login/login.component';
import { ForgotComponent } from '../../pages/auth/forgot/forgot.component';
import { VerifyComponent } from '../../pages/auth/verify/verify.component';
export const AuthenticationLayoutRoutingModule : Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'forgot', component: ForgotComponent },
{ path: 'verify/invite/:_hash', component: VerifyComponent},
{ path: '**', redirectTo: 'login' }
]
auth-layout.module.ts
@NgModule({
declarations: [
LoginComponent,
ForgotComponent,
VerifyComponent
],
imports: [
CommonModule,
RouterModule.forChild(AuthenticationLayoutRoutingModule),
ThemeModule,
NbAlertModule,
FormsModule,
ReactiveFormsModule,
NbPopoverModule,
NbCardModule,
NbIconModule
],
})
export class AuthenticationLayoutModule { }
我试图从验证组件导航到登录组件,下面是代码
constructor(private router: Router, private activatedRoute: ActivatedRoute, private authService: AuthService, private toastrService: NbToastrService) {}
ngOnInit() {
this.router.navigate['/auth/login'];
}
但是如果我使用下面的代码,它将正确重定向
<a [routerLink]="['/auth/login']">Click to redirect</a>
如果我做错了什么,请告诉我。