简体   繁体   English

angular 2路由器引导懒惰模块

[英]angular 2 router guide lazy module

I have some unclarities about angular 2 router guide . 关于角度2 路由器指南我有一些不清楚。

CanDeactivateGuard is used in CrisisCenterRoutingModule which is part of CrisisCenterModule which is lazy loaded by AppRoutingModule (loadChildren: 'app/crisis-center/crisis-center.module#CrisisCenterModule'). CanDeactivateGuard用于CrisisCenterRoutingModule,它是由AppRoutingModule(loadChildren:'app / crisis-center / crisis-center.module#CrisisCenterModule')延迟加载的CrisisCenterModule的一部分。

The main question: why should CanDeactivateGuard be declared in AppRoutingModule providers? 主要问题:为什么要在AppRoutingModule提供程序中声明CanDeactivateGuard? why not to CrisisCenterRoutingModule providers? 为什么不到CrisisCenterRoutingModule提供商?

The guide's answer is: we also need to add the Guard to our main AppRoutingModule providers so the Router can inject it during the navigation process. 该指南的答案是:我们还需要将Guard添加到我们的主AppRoutingModule提供程序中,以便路由器可以在导航过程中注入它。

Well, it seems that indeed CanDeactivateGuard could be provided by CrisisCenterRoutingModule too instead of AppRoutingModule. 好吧,似乎确实CanDeactivateGuard也可以由CrisisCenterRoutingModule而不是AppRoutingModule提供。 Now the question is why then the guide pointed AppRoutingModule as the one to provide CanDeactivateGuard? 现在的问题是为什么然后指南指出AppRoutingModule是提供CanDeactivateGuard的那个? The answer I guess is that the CanDeactivateGuard is a service useful to any module and because lazy loaded modules have their own root injector this means it would be created an additional instance of CanDeactivateGuard for any lazy load module that would provide it too. 答案我猜是CanDeactivateGuard是一个对任何模块都有用的服务,因为延迟加载的模块有自己的根注入器,这意味着它将为任何提供它的延迟加载模块创建一个额外的CanDeactivateGuard实例。 CanDeactivateGuard has no state so it's useless to have more than one instance of it so it's better to put it in AppRoutingModule. CanDeactivateGuard没有状态,因此拥有多个实例是没用的,所以最好把它放在AppRoutingModule中。

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

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