[英]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. 我的问题是浏览器按钮,无论用户单击
back
, forward
还是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.