mixins.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { uniAppHook } from './config';
  2. import H5init from '../vueRouter/init';
  3. import { appInit, removeBackPressEvent, pageIsHeadBack } from '../appRouter/init';
  4. import appletsInit from '../appletsRouter/init';
  5. import { appPlatform } from './util';
  6. import { proxyIndexHook } from '../appRouter/hooks';
  7. import { appletsProxyIndexHook } from '../appletsRouter/hooks';
  8. /**
  9. * 获取一些需要在各个平台混入的事件
  10. * @param {Object} Router 当前原始路由对象
  11. */
  12. const getMixins = function (Router) {
  13. return {
  14. H5: {
  15. beforeCreate() {
  16. if (this.$options.router) {
  17. H5init(Router.$root, this.$options.router, this);
  18. }
  19. },
  20. },
  21. APP: {
  22. onLaunch() {
  23. uniAppHook.onLaunched = true; // 标志已经触发了 onLaunch 事件
  24. appInit.call(this, Router.$root);
  25. },
  26. onLoad() {
  27. // 第一个页面 拦截所有生命周期
  28. if (uniAppHook.onLaunched && !uniAppHook.pageReady) {
  29. uniAppHook.onLaunched = false;
  30. proxyIndexHook.call(this, Router.$root);
  31. }
  32. removeBackPressEvent(this.$mp.page, this.$options); // 移除页面的onBackPress事件
  33. },
  34. onBackPress(...args) {
  35. return pageIsHeadBack.call(Router.$root, this.$mp.page, this.$options, args);
  36. },
  37. },
  38. APPLETS: {
  39. onLaunch() {
  40. uniAppHook.onLaunched = true; // 标志已经触发了 onLaunch 事件
  41. appletsInit.call(this, Router.$root);
  42. },
  43. onLoad() {
  44. if (uniAppHook.onLaunched && !uniAppHook.pageReady) { // 必须是第一个页面
  45. uniAppHook.onLaunched = false;
  46. appletsProxyIndexHook.call(this, Router.$root);
  47. }
  48. },
  49. },
  50. };
  51. };
  52. const initMixins = function (Vue, Router) {
  53. Vue.mixin({
  54. ...getMixins(Router)[appPlatform(true)],
  55. });
  56. };
  57. export default initMixins;