在路由提供者开始执行解析列表之前执行函数

Execute function before routeprovider starts executing resolve list

我正在开发一个与服务器缓存一起使用的 Angular 项目。 我的计划是:

  1. 创建一个服务 (CacheService),向服务器询问缓存状态并将此信息存储到 localStorage。
  2. 因此,在 routeProvider 解析对象中调用的其他服务可以向 CacheService 询问其缓存状态,如果缓存状态已过时,则调用服务器获取新数据,或者,如果缓存未更改,则使用其本地缓存数据

所以问题是:有没有办法在执行解析器之前执行异步函数?或者“在解析器中有什么方法可以在执行之前等待其他解析器?

提前致谢

$route 服务提供了一个 $routeChangeStart 事件,您可以使用它来完成此操作。

Broadcasted before a route change. At this point the route services starts resolving all of the dependencies needed for the route change to occur. Typically this involves fetching the view template as well as any dependencies defined in resolve route property. Once all of the dependencies are resolved $routeChangeSuccess is fired.

The route change (and the $location change that triggered it) can be prevented by calling preventDefault method of the event. See $rootScope.Scope for more details about event object.

您应该能够使用此事件进入路由流,执行缓存检查,然后决定下一步做什么(使用缓存、中止路由等)