无法匹配任何路由。URL段: ‘auth/login’

huangapple go评论110阅读模式
英文:

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: &#39;auth/login&#39;.

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 &#39;@angular/core&#39;;
import { RouterModule, Routes } from &#39;@angular/router&#39;;

const routes: Routes = [
  {
    path: &#39;&#39;,
    loadChildren: () =&gt; import(&#39;./web/auth/auth.module&#39;).then(m =&gt; m.AuthModule)
  },
  {
    path: &#39;account&#39;,
    loadChildren: () =&gt; import(&#39;./web/account/account.module&#39;).then(m =&gt; m.AccountModule)
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html:

&lt;p&gt;app component works!&lt;/p&gt;
&lt;router-outlet&gt;&lt;/router-outlet&gt;

auth-routing.module:

import { NgModule } from &#39;@angular/core&#39;;
import { RouterModule, Routes } from &#39;@angular/router&#39;;
import { LoginComponent } from &#39;./login/login.component&#39;;
import { RegisterComponent } from &#39;./register/register.component&#39;;
import { ForgotPasswordComponent } from &#39;./forgot-password/forgot-password.component&#39;;
import { ResetPasswordComponent } from &#39;./reset-password/reset-password.component&#39;;

const routes: Routes = [
  { path: &#39;login&#39;, component: LoginComponent },
  { path: &#39;register&#39;, component: RegisterComponent },
  { path: &#39;forgot-password&#39;, component: ForgotPasswordComponent },
  { path: &#39;reset-password&#39;, component: ResetPasswordComponent },
  {
    path: &#39;**&#39;, redirectTo: &#39;login&#39;
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AuthRoutingModule { }

auth.component.html:

&lt;p&gt;auth works!&lt;/p&gt;
&lt;router-outlet&gt;&lt;/router-outlet&gt;

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: &#39;&#39;,
        redirectTo: &#39;auth&#39;,
        pathMatch: &#39;full&#39;
      },
      {
        path: &#39;auth&#39;,
        loadChildren: () =&gt; import(&#39;./web/auth/auth.module&#39;).then(m =&gt; m.AuthModule)
      },
      {
        path: &#39;account&#39;,
        loadChildren: () =&gt; import(&#39;./web/account/account.module&#39;).then(m =&gt; m.AccountModule)
      }
    ];

Update your routes array in auth-routing.module.ts to below array

const routes: Routes = [
  { path: &#39;&#39;, redirectTo: &#39;login&#39;, pathMatch: &#39;full&#39; }
  { path: &#39;login&#39;, component: LoginComponent },
  { path: &#39;register&#39;, component: RegisterComponent },
  { path: &#39;forgot-password&#39;, component: ForgotPasswordComponent },
  { path: &#39;reset-password&#39;, component: ResetPasswordComponent },
  {
    path: &#39;**&#39;, redirectTo: &#39;login&#39;
  }
];

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: &#39;&#39;,
        redirectTo: &#39;auth&#39;,
        pathMatch: &#39;full&#39;
      },
      {
        path: &#39;auth&#39;,
        loadChildren: () =&gt; import(&#39;./web/auth/auth.module&#39;).then(m =&gt; m.AuthModule)
      },
      {
        path: &#39;account&#39;,
        loadChildren: () =&gt; import(&#39;./web/account/account.module&#39;).then(m =&gt; m.AccountModule)
      }
    ];

huangapple
  • 本文由 发表于 2023年1月9日 18:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定