index.js 1021 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { install } from './install';
  2. import { hackUniRoute } from './hackRoute';
  3. import { handleGlobalHooksQueue } from './handleHooks';
  4. import { registerHook } from './utils';
  5. export default class uniRouteGuards {
  6. constructor() {
  7. // 初始化数据
  8. this.beforeHooks = [];
  9. this.afterHooks = [];
  10. this.errorCbs = [];
  11. hackUniRoute.call(this, handleGlobalHooksQueue);
  12. }
  13. /**
  14. * 注册 全局前置守卫
  15. * @param {Function} callback 回调函数
  16. */
  17. beforeEach(callback) {
  18. return registerHook(this.beforeHooks, callback);
  19. }
  20. /**
  21. * 注册 全局后置守卫
  22. * @param {Function} callback 回调函数
  23. */
  24. afterEach(callback) {
  25. return registerHook(this.afterHooks, callback);
  26. }
  27. /**
  28. * 注册 错误回调
  29. * @param {Function} errCb 错误回调函数
  30. */
  31. onError(errCb) {
  32. return registerHook(this.errorCbs, errCb);
  33. }
  34. }
  35. // 添加 Vue.use 功能
  36. uniRouteGuards.install = install;