Angular 角度路由错误:无法匹配任何路由
我正在尝试在我的angular应用程序中实现延迟加载。我已将我的申请分为两部分;认证模块和家庭模块。我在app-routing.Module.ts中定义了我的路线,如下所示:Angular 角度路由错误:无法匹配任何路由,angular,angular-routing,angular-router,Angular,Angular Routing,Angular Router,我正在尝试在我的angular应用程序中实现延迟加载。我已将我的申请分为两部分;认证模块和家庭模块。我在app-routing.Module.ts中定义了我的路线,如下所示: import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; const routes: Routes = [ { path: '', redirectTo: '/authe
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '',
redirectTo: '/authentication/signin',
pathMatch: 'full'
},
{
path: '',
children: [
{
path: 'authentication',
loadChildren: () => import('./modules/authentication/authentication.module').then(m => m.AuthenticationModule)
},
{
path: 'home',
loadChildren: () => import('./modules/home/home.module').then(m => m.HomeModule)
}
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
然后在身份验证和主模块中,我定义了另一组路由
authentication.module.ts:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { SignupComponent } from './signup/signup.component';
import { SigninComponent } from './signin/signin.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { SharedModule } from '../shared/shared.module';
export const authenticationRoutes = [
{
path: '',
redirectTo: 'signup',
pathMatch: 'full'
},
{
path: 'signin',
component: SigninComponent
},
{
path: 'signup',
component: SignupComponent
},
{
path: 'forgot-password',
component: ForgotPasswordComponent
}
];
@NgModule({
declarations: [
SignupComponent,
SigninComponent,
ForgotPasswordComponent
],
imports: [
CommonModule,
SharedModule,
RouterModule.forChild(authenticationRoutes)
]
})
export class AuthenticationModule { }
import { HomeComponent } from './home/home.component';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
export const authenticationRoutes = [
{
path: 'home',
component: HomeComponent
}
];
@NgModule({
declarations: [],
imports: [
CommonModule
]
})
export class HomeModule { }
home.module.ts:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { SignupComponent } from './signup/signup.component';
import { SigninComponent } from './signin/signin.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { SharedModule } from '../shared/shared.module';
export const authenticationRoutes = [
{
path: '',
redirectTo: 'signup',
pathMatch: 'full'
},
{
path: 'signin',
component: SigninComponent
},
{
path: 'signup',
component: SignupComponent
},
{
path: 'forgot-password',
component: ForgotPasswordComponent
}
];
@NgModule({
declarations: [
SignupComponent,
SigninComponent,
ForgotPasswordComponent
],
imports: [
CommonModule,
SharedModule,
RouterModule.forChild(authenticationRoutes)
]
})
export class AuthenticationModule { }
import { HomeComponent } from './home/home.component';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
export const authenticationRoutes = [
{
path: 'home',
component: HomeComponent
}
];
@NgModule({
declarations: [],
imports: [
CommonModule
]
})
export class HomeModule { }
因此,每当我尝试执行代码时,我都希望有一个空URL将我重定向到注册,但是无论我是否指定了路由,我最终都会遇到相同的错误,即:
错误:未捕获(承诺中):错误:无法匹配任何路由。URL段:“身份验证/登录”
错误:无法匹配任何路由。URL段:“身份验证/登录”
如何解决此问题?应用程序路由模块中存在问题。当检查匹配的路径时,两个路径都为空,并且由于您在路径为空时提供了第一个重定向,因此应用程序永远不会到达任何其他路径。最好的方法是不要将身份验证和主模块指定为子模块,并在看到下面的“身份验证”路径时重新路由到身份验证
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: 'authentication',
loadChildren: () => import('./modules/authentication/authentication.module').then(m => m.AuthenticationModule)
},
{
path: 'home',
loadChildren: () => import('./modules/home/home.module').then(m => m.HomeModule)
},
{
path: '**', //If path doesn't match anything reroute to /authentication/signin
redirectTo: '/authentication/signin',
pathMatch: 'full'
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
不知何故,我还是犯了同样的错误。我不确定哪里出了问题。将重定向代码移动到身份验证模块中的最后一个,您在主模块中缺少RouterModule.forChild