英文:
Cannot match any routes. URL Segment: 'auth/login'
问题
我是Angular的新手,一直在处理我的第一个项目的路由。当我尝试访问我的登录页面时遇到了问题,出现了错误Cannot match any routes. URL Segment: 'auth/login'
。
我的文件夹结构如下:
app
|
+-- web
| |
| +-- account
| +-- auth
| | |
| | +-forgot-password
| | +-login
| | +-register
| | +-reset-password
| +-- auth-routing.module.ts
| +-- auth-module.ts
+-- app-routing.module.ts
+-- app-module.ts
浏览器只显示app component works!
的消息,这是在创建组件时自动生成的,甚至不显示auth works!
的消息,这个消息在auth
文件夹中找到。
app-routing.module:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{
path: '',
loadChildren: () => import('./web/auth/auth.module').then(m => m.AuthModule)
},
{
path: 'account',
loadChildren: () => import('./web/account/account.module').then(m => m.AccountModule)
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.component.html:
<p>app component works!</p>
<router-outlet></router-outlet>
auth-routing.module:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { ResetPasswordComponent } from './reset-password/reset-password.component';
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'forgot-password', component: ForgotPasswordComponent },
{ path: 'reset-password', component: ResetPasswordComponent },
{
path: '**',
redirectTo: 'login'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AuthRoutingModule { }
auth.component.html:
<p>auth works!</p>
<router-outlet></router-outlet>
我尝试过更改路径的顺序并添加空路径和通配符路径,但没有取得太大成功。
英文:
I'm new to Angular, and I've been dealing with the routing of my first project. I'm having trouble once I try to access my login page and I come across the error Cannot match any routes. URL Segment: 'auth/login'
.
My folder structure look like this:
app
|
+-- web
| |
| +-- account
| +-- auth
| | |
| | +-forgot-password
| | +-login
| | +-register
| | +-reset-password
| +-- auth-routing.module.ts
| +-- auth-module.ts
+-- app-routing.module.ts
+-- app-module.ts
The browser just displays the message app component works!
that is generated automatically once you create a component but doesn't even show the message auth works!
that is found in the auth
folder.
app-routing.module:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{
path: '',
loadChildren: () => import('./web/auth/auth.module').then(m => m.AuthModule)
},
{
path: 'account',
loadChildren: () => import('./web/account/account.module').then(m => m.AccountModule)
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.component.html:
<p>app component works!</p>
<router-outlet></router-outlet>
auth-routing.module:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { ForgotPasswordComponent } from './forgot-password/forgot-password.component';
import { ResetPasswordComponent } from './reset-password/reset-password.component';
const routes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'forgot-password', component: ForgotPasswordComponent },
{ path: 'reset-password', component: ResetPasswordComponent },
{
path: '**', redirectTo: 'login'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AuthRoutingModule { }
auth.component.html:
<p>auth works!</p>
<router-outlet></router-outlet>
I've tried to change the order of paths and adding the empty and the wildcard route without much success.
答案1
得分: 2
以下是翻译好的部分:
在 app-routing.module.ts 文件中更新您的路由数组如下:
const routes: Routes = [
{
path: '',
redirectTo: 'auth',
pathMatch: 'full'
},
{
path: 'auth',
loadChildren: () => import('./web/auth/auth.module').then(m => m.AuthModule)
},
{
path: 'account',
loadChildren: () => import('./web/account/account.module').then(m => m.AccountModule)
}
];
在 auth-routing.module.ts 文件中更新您的路由数组如下:
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'forgot-password', component: ForgotPasswordComponent },
{ path: 'reset-password', component: ResetPasswordComponent },
{
path: '**',
redirectTo: 'login'
}
];
谢谢!
英文:
Update your routes array in app-routing.module.ts with below array.
const routes: Routes = [
{
path: '',
redirectTo: 'auth',
pathMatch: 'full'
},
{
path: 'auth',
loadChildren: () => import('./web/auth/auth.module').then(m => m.AuthModule)
},
{
path: 'account',
loadChildren: () => import('./web/account/account.module').then(m => m.AccountModule)
}
];
Update your routes array in auth-routing.module.ts to below array
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' }
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'forgot-password', component: ForgotPasswordComponent },
{ path: 'reset-password', component: ResetPasswordComponent },
{
path: '**', redirectTo: 'login'
}
];
Thanks!
答案2
得分: 0
只需在app.routing.ts中添加认证(auth)部分:
const routes: Routes = [
{
path: '',
redirectTo: 'auth',
pathMatch: 'full'
},
{
path: 'auth',
loadChildren: () => import('./web/auth/auth.module').then(m => m.AuthModule)
},
{
path: 'account',
loadChildren: () => import('./web/account/account.module').then(m => m.AccountModule)
}
];
英文:
Just add auth in app.routing.ts
const routes: Routes = [
{
path: '',
redirectTo: 'auth',
pathMatch: 'full'
},
{
path: 'auth',
loadChildren: () => import('./web/auth/auth.module').then(m => m.AuthModule)
},
{
path: 'account',
loadChildren: () => import('./web/account/account.module').then(m => m.AccountModule)
}
];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论