hooks.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { appPlatform, isH5 } from '../helpers/util';
  2. // #ifdef H5
  3. import H5 from '../patch/h5-patch';
  4. const H5PATCH = new H5(isH5());
  5. // #endif
  6. export const registerHook = function (list, fn) {
  7. list.push(fn);
  8. return () => {
  9. const i = list.indexOf(fn);
  10. if (i > -1) list.splice(i, 1);
  11. };
  12. };
  13. /**
  14. * 注册全局Router生命钩子
  15. */
  16. export const registerRouterHooks = function () {
  17. registerHook(this.lifeCycle.routerbeforeHooks, function () {
  18. return new Promise(async (resolve) => {
  19. this.CONFIG.routerBeforeEach(); // 触发暴露给开发者的生命钩子
  20. if (appPlatform(true) === 'H5') {
  21. H5PATCH.on('toogle', 'startLodding');
  22. }
  23. return resolve(true);
  24. });
  25. });
  26. registerHook(this.lifeCycle.routerAfterHooks, function (res = {}) {
  27. if (res.H5Intercept !== true) {
  28. this.CONFIG.routerAfterEach(); // 触发暴露给开发者的生命钩子
  29. }
  30. if (appPlatform(true) === 'H5') {
  31. H5PATCH.on('toogle', 'stopLodding');
  32. }
  33. return true;
  34. });
  35. };