Javascript 错误页面重定向在角度延迟加载时无法正常工作

Javascript 错误页面重定向在角度延迟加载时无法正常工作,javascript,angular,angular-routing,angular-router,Javascript,Angular,Angular Routing,Angular Router,我陷入了困境,需要专家的帮助。我的应用程序是建立在延迟加载路由之上的,所有模块都有自己的路由文件。除了错误页面重定向之外,一切正常。如果我点击localhost:4200/error,我可以在几秒钟内看到错误页面,然后自动将其重定向回主页 应用程序路由 const routes: Routes = [ { path: '', redirectTo: 'home', pathMatch: 'full', canActivate: [AuthGuardService] }, { path: 'hom

我陷入了困境,需要专家的帮助。我的应用程序是建立在延迟加载路由之上的,所有模块都有自己的路由文件。除了错误页面重定向之外,一切正常。如果我点击
localhost:4200/error
,我可以在几秒钟内看到错误页面,然后自动将其重定向回主页

应用程序路由

const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full',
canActivate: [AuthGuardService]
},
{
path: 'home',
loadChildren: () => import('../home/home.module').then((m) => m.HomeModule),
canActivate: [AuthGuardService],
},
{
path: 'dashboard',
loadChildren: () => import('../another/another.module').then((m) => m.AnotherModule),
canActivate: [AuthGuardService],
},
{ path: '**', redirectTo: '/error' } ];

export const Routing: ModuleWithProviders = RouterModule.forRoot(routes);
错误模块:启用正常路由而不是延迟加载

const routes: Routes = [{ path: 'error', component: ErrorComponent }];

export const ErrorRoutingModule: ModuleWithProviders = RouterModule.forRoot(routes);
两个模块都像往常一样导入到应用程序模块:

imports:[
  .........
  ErrorModule,
   Routing
 ]
我用的是角度8。任何帮助都会很有帮助

更新: 发现问题,实际上在成功登录后,我将用户从我的服务重定向到主页

this.service.login().subscribe(response=> { if(response.valid){this.router.navigate['/home'];}

需要帮助来解决这个问题。所以它可能是一个有效的用户,但输入了一个错误的url,在这种情况下,它应该返回到
error
页面。正在从我的
应用程序组件调用此登录服务。请尝试使用以下示例:

{ path: '404', component: ErrorComponent },
{ path: '**', redirectTo: '/404' },
错误组件ts


您能解释一下吗?除非您在错误组件中显式编写了代码,以便在几秒钟后进行路由,否则您不会返回到任何其他页面。。。您可以添加错误组件代码以获得更清晰的图片。我的错误组件上没有任何内容。只是空白的
constructor
函数,html页面有一些静态内容,我想我读错了。。。你想重定向,或者它正在发生,你想阻止它?它正在发生,但它不会停留在该页面上。首先是重定向,可以看到内容,但几秒钟后它会自动重定向回主页。那么你的意思是,你首先进入错误页面,然后自动重定向到你不希望发生的主页?。。。如果我的理解有误,请纠正我。
export class ErrorComponent implements OnInit {
  
  public msToRelocate: number = 5000;

  constructor(private router: Router) { }

  ngOnInit() {
    Observable.of(true).pipe(
    delay(msToRelocate),
    tap(() => {
        this.goToHomePage();
      }),
    );
  }

  public goToHomePage() {
    this.router.navigate(['']);
  }

}