简体   繁体   English

Angular 5延迟加载的应用程序和浏览器导航按钮

[英]Angular 5 lazy loaded app and browser navigation buttons

I have a big app which is mostly created by composing a lot of lazily loaded modules. 我有一个很大的应用程序,主要是由许多延迟加载的模块组成的。 My problem is with the browser buttons, whether the user clicks on back , forward or refresh my app crashes. 我的问题是浏览器按钮,无论用户单击backforward还是refresh我的应用程序崩溃。

As far as I've researched, I could somehow catch those events, stop them from propagating and handle the event myself. 据我研究,我可以以某种方式捕捉到这些事件,阻止它们传播并自己处理该事件。

Are there any other good solutions for this? 还有其他好的解决方案吗? What aspects should I take into consideration? 我应该考虑哪些方面?

Edit due to comments 根据评论进行编辑

I don't think this would be an angular router issue. 我不认为这是一个有角度的路由器问题。 Thing is, my app programatically changes the router configuration at runtime. 事实是,我的应用程序在运行时以编程方式更改了路由器配置。 Therefore when an user tries to reload or such, he tries to enter a path not yet existant. 因此,当用户尝试重新加载等时,他尝试输入尚不存在的路径。 I could somehow play with the datastore and re-inject the router config before navigation but I don't think that would be wise. 我可以以某种方式使用数据存储,并在导航之前重新插入路由器配置,但我认为这并不明智。

By crashing, I simply mean errors such as couldn't find route 'x\\a\\b' . 通过崩溃,我只是表示错误,例如couldn't find route 'x\\a\\b'

The current idea I have now, is to catch the events of the browser buttons and handle them myself, changing the way they work but I'm not sure whether this would even be possible (something like what gmail does I guess). 我现在的当前想法是捕获浏览器按钮的事件并自己处理它们,改变它们的工作方式,但是我不确定这是否可能(类似于gmail我猜的东西)。

I also have a tremendous number of routes and to handle route changes i use the routeEvent 我也有大量的路线,并且使用routeEvent来处理路线更改

import {ActivatedRoute, Event, NavigationEnd, NavigationError, NavigationStart, Router} from '@angular/router';
...
  routerEvent;

  constructor(private router: Router,
              private route: ActivatedRoute) {
    this.routerEvent = router.events.subscribe((event: Event) => {
      if (event instanceof NavigationStart) {
        // Show loading indicator
      }

      if (event instanceof NavigationEnd) {
        // Hide loading indicator
        this.init();
      }

      if (event instanceof NavigationError) {
        // Hide loading indicator
        // Present error to user
      }
    });
  }

As the event names suggest, NavigationStart is triggered when you freshly load your route, NavigationEnd is triggered when the route navigation load is complete and NavigationError when your route fails to load. 作为本次活动名称所暗示的,NavigationStart被触发,当你刚加载路线,NavigationEnd的路径引导加载完成且NavigationError时,当你的路线加载失败触发。

It's also a good idea to unsubscribe when nolonger needed. 在不再需要时退订也是一个好主意。

  ngOnDestroy(): void {
    this.routerEvent.unsubscribe();
  }

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

相关问题 Angular 延迟加载的组件在导航时重新渲染 - Angular Lazy Loaded Component Re-renders on navigation Angular 更改检测未在初始导航的延迟加载页面上发生 - Angular Change Detection not happening on Lazy loaded pages on initial navigation 依赖项注入错误随导航流而变化(延迟加载的Angular模块) - Dependency injection errors change with navigation flow (lazy loaded Angular modules) 应用模块和延迟加载的模块之间的通信(角度4) - Communication between app module and lazy loaded modules in angular 4 对于 Angular 中的延迟加载模块,是否有类似 APP_INITIALIZER 的东西? - Is there anything like APP_INITIALIZER for lazy loaded modules in Angular? Angular APP_INITIALIZER 是否在延迟加载模块内部工作 - Angular Does APP_INITIALIZER work inside of lazy loaded modules 角应用程序级服务未在延迟加载的路由中实例化 - Angular app-level service not instantiating in lazy-loaded route Angular 9 Cordova App 混合应用和延迟加载模块 - Angular 9 Cordova App Hybrid Application and Lazy Loaded Modules 列出 Angular 9 应用懒加载模块的所有路由 - List all routes of Angular 9 app with lazy loaded modules Angular 4路由器:直接浏览器导航重启app - Angular 4 router: direct browser navigation restarts app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM