angular的@uirouter的lazyload功能,在使用onBefore hook时,如果不进行一定设置是监听不到lazyload的hook的。原来是因为lazy loade hook的优先级是0,要设置此hook需要在onBefore中设置priority: 1

在uirouter的github的一个issue中给出了解释:传送门

You should be able to implement this with these existing primitives:
* lazyLoad property on a state
* An onBefore Transition Hook when entering a lazyload state
* A Transition promise
javascript
const criteria = { entering: (state) => !!state.lazyLoad };
const options = { priority: 1 };
const hook = trans => {
console.log('Lazy load starting...');
const done = () => console.log('Lazy load complete!');
trans.promise.then(done, done)
};
uiRouter.transitionService.onBefore(criteria, hook, options);

Here’s an example: https://stackblitz.com/edit/ui-router-angular-ysa7jk?file=app%2Fapp.module.ts

Note: priority: 1 is necessary because the lazy load hook itself is a priority 0 onBefore hook.

分类: angularjs

发表评论

电子邮件地址不会被公开。 必填项已用*标注