简体   繁体   English

Angular 4无法匹配任何路线

[英]Angular 4 Cannot match any routes

I using ng serve and have such routing config 我使用ng serve并具有这样的路由配置

export const appRoutes: Routes = [
  {
    path: '',
    children: [
      {
        path: '',
        component: AuthComponent,
        children: [
          {
            path: 'login',
            component: LoginComponent
          },
          {
            path: 'registration',
            component: RegistrationComponent
          }
        ]
      },
      {
        path: 'restore',
        component: RestoreComponent
      },
      {
        path: 'newpass',
        component: NewPassComponent
      }
    ]
  }
];

and two button in auth.component.html 和auth.component.html中的两个按钮

<nav>
  <ul>
    <li><a routerLink="/login" routerLinkActive="active-link">Login</a></li>
    <li><a routerLink="/registration" routerLinkActive="active-link">Sign up</a></li>
  </ul>
</nav>

when I click on login link, angular navigate me to login page without errors, but when I reload localhost:4200/login page, I got this error 当我单击登录链接时,按角度导航到登录页面时没有错误,但是当我重新加载localhost:4200 /登录页面时,出现此错误

Console screen 控制台画面

but page loaded successful and components will work. 但页面加载成功,组件将正常工作。

So, how I can fix it and why angular make second NavigationStart to /login/(login) ? 那么,我该如何解决它,又为什么要使第二个NavigationStart进入/ login /(login)呢?

You don't need to use children of path "". 您无需使用路径“”的子代。 Try to use this: 尝试使用此:

export const appRoutes: Routes = [
  {
    path: 'pathToAuth', // change to your path
    component: AuthComponent,
    children: [
      {
        path: 'login',
        component: LoginComponent
      },
      {
        path: 'registration',
        component: RegistrationComponent
      }
    ]
  },
  {
    path: 'restore',
    component: RestoreComponent
  },
  {
    path: 'newpass',
    component: NewPassComponent
  }
];

ok. 好。 I solved this problem. 我解决了这个问题。 In AuthComponent I have this code 在AuthComponent中,我有此代码

let isAuthorized = await this.checkAuthorization();
if (isAuthorized) {
  this.router.navigate(['dashboard'], {relativeTo: this.route});
} else {
  this.router.navigate(['login'], {relativeTo: this.route});
}

so I make redirect from /login to /login/login but route config doesnt have this path 所以我将重定向从/ login重定向到/ login / login,但路由配置没有此路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM