添加了通配符路由,但仍适用于未定义的路由无法获取/

问题描述

我在项目中使用Angular 6。我添加通配符路由,但是当我转到任何未定义的路由角度时,浏览器在浏览器中给出Cannot GET /<path>时仍然面临错误

下面是我的路线,所有其他定义的路线都可以正常运行,但通配符不起作用

const AppRoutes: Routes = [
 { path: '',redirectTo: 'login',pathMatch: 'full' },{ path: "register",component: RegisterComponent },{ path: "login",component: LoginComponent },{ path: "dashboard",component: DashboardComponent,canActivate: [AuthGuard] },{ path:"backup",component:BackupComponent},{ path: '**',component: NotFoundComponent },];

认情况下会导入路由。

imports: [
 browserModule,ReactiveFormsModule,NgxPaginationModule,.
 .
 . 
 RouterModule.forRoot(AppRoutes),],

由于某些原因,我无法使用基于哈希的路由

//Can not use hash base routing in my case
RouterModule.forRoot(AppRoutes,{ useHash: true }),

我相信通配符应该在没有基于散列的路由的情况下工作。让我知道我要去哪里了。

解决方法

您定义的路线似乎没有问题。但是,check this article对于基于散列的路由存在问题。

,

您可以尝试通过两种方式解决该问题:

  1. 尝试更改父路线和通配符路线的顺序,例如
const AppRoutes: Routes = [
 { path: "register",component: RegisterComponent },{ path: "login",component: LoginComponent },{ path: "dashboard",component: DashboardComponent,canActivate: [AuthGuard] },{ path:"backup",component:BackupComponent},{ path: '',redirectTo: 'login',pathMatch: 'full' },{ path: '**',component: NotFoundComponent },];
  1. redirectTo使用NotFoundComponent

请查看本文以获取更多详细信息WildCardRoutes

谢谢

,

我在NodeJS中使用后端运行应用程序,通过ng-build和node app.js在同一端口上运行Angular和Node。当我用ng服务运行angular时,它可以正常工作,但不适用于ng build,但在生产中应有尽有看起来不错,通配符可以按预期工作。