Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

肖康 / H5_2.0

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • H5_2.0
  • node_modules
  • _uni-simple-router@1.5.5@uni-simple-r...
  • helpers
  • mixins.js
mixins.js 2.07 KB
肖康's avatar
first init
ddbafbda
 
肖康 committed 2 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
import { uniAppHook } from './config';
import H5init from '../vueRouter/init';
import { appInit, removeBackPressEvent, pageIsHeadBack } from '../appRouter/init';
import appletsInit from '../appletsRouter/init';
import { appPlatform } from './util';
import { proxyIndexHook } from '../appRouter/hooks';
import { appletsProxyIndexHook } from '../appletsRouter/hooks';

/**
 * 获取一些需要在各个平台混入的事件
 * @param {Object} Router 当前原始路由对象
 */
const getMixins = function (Router) {
    return {
        H5: {
            beforeCreate() {
                if (this.$options.router) {
                    H5init(Router.$root, this.$options.router, this);
                }
            },
        },
        APP: {
            onLaunch() {
                uniAppHook.onLaunched = true;	// 标志已经触发了 onLaunch 事件
                appInit.call(this, Router.$root);
            },
            onLoad() {
                // 第一个页面 拦截所有生命周期
                if (uniAppHook.onLaunched && !uniAppHook.pageReady) {
                    uniAppHook.onLaunched = false;
                    proxyIndexHook.call(this, Router.$root);
                }
                removeBackPressEvent(this.$mp.page, this.$options); // 移除页面的onBackPress事件
            },
            onBackPress(...args) {
                return pageIsHeadBack.call(Router.$root, this.$mp.page, this.$options, args);
            },
        },
        APPLETS: {
            onLaunch() {
                uniAppHook.onLaunched = true;	// 标志已经触发了 onLaunch 事件
                appletsInit.call(this, Router.$root);
            },
            onLoad() {
                if (uniAppHook.onLaunched && !uniAppHook.pageReady) {	// 必须是第一个页面
                    uniAppHook.onLaunched = false;
                    appletsProxyIndexHook.call(this, Router.$root);
                }
            },
        },
    };
};

const initMixins = function (Vue, Router) {
    Vue.mixin({
        ...getMixins(Router)[appPlatform(true)],
    });
};

export default initMixins;