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
BlameHistoryPermalink
Switch branch/tag
  • H5_2.0
  • node_modules
  • uni-simple-router
  • vueRouter
  • init.js
  • 肖康's avatar
    first init · ddbafbda
    肖康 committed 2 years ago
    ddbafbda
init.js 2.62 KB
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 61 62 63 64 65 66 67 68
import { afterHooks, beforeHooks, registerRouter } from './concat';
import { fromatRoutes } from './util';
import { err, warn } from '../helpers/warn';
import { proxyEachHooks } from './proxy/proxy';
/**
 * 重写掉H5端 uni-app原始存在的bug
 *
 * @param {Object} Router
 */
const rewriteUniFun = function (Router) {
    if (Router.CONFIG.h5.rewriteFun === false) { // 不需要重写
        return false;
    }
    uni.reLaunch = function ({
        url,
    }) {
        if (url === '/') {
            warn('H5端 uni.reLaunch(\'/\')时 默认被重写了! 你可以使用 this.$Router.replaceAll() 或者 uni.reLaunch(\'/\'?xxx)');
            // eslint-disable-next-line
            if (history.length > 1) { // 只有在有历史记录的时候才返回  不然直接返回首页
                return Router.back();
            }
            return Router.replaceAll('/');
        }
        const path = url.match(/^[^?]+|(\/)/)[0];
        try {
            const query = {};
            url.replace(/([^?&]+)=([^?&]+)/g, (s, v, k) => {
                query[v] = decodeURIComponent(k);
                return `${k}=${v}`;
            });
            Router.replaceAll({
                path,
                query,
            });
        } catch (e) {
            err(`${url}解析失败了....  试试 this.$Router.replaceAll() 吧`);
        }
    };
    uni.navigateBack = function (delta) {
        let backLayer = delta;
        if (delta.constructor === Object) { // 这种可能就只是uni-app自带的返回按钮,还有种可能就是开发者另类传递的
            backLayer = 1;
        }
        Router.back(backLayer, delta);
    };
};
/**
 * 拦截并注册vueRouter中的生命钩子,路由表解析
 * @param {Object} Router
 * @param {vueRouter} vueRouter
 */
const init = function (Router, vueRouter) {
    const CONFIG = Router.CONFIG.h5;
    vueRouter.afterHooks = proxyEachHooks(Router, 'afterHooks', afterHooks);
    vueRouter.beforeHooks = proxyEachHooks(Router, 'beforeHooks', beforeHooks);
    const objVueRoutes = fromatRoutes(vueRouter.options.routes, false, {}); // 返回一个格式化好的routes 键值对的形式
    const objSelfRoutes = fromatRoutes(Router.CONFIG.routes, true, CONFIG);
    Router.vueRoutes = objVueRoutes; // 挂载vue-routes到当前的路由下
    Router.selfRoutes = {
        ...Router.selfRoutes || {},
        ...objSelfRoutes,
    }; // 挂载self-routes到当前路由下
    Router.$route = vueRouter; // 挂载vue-router到$route
    rewriteUniFun(Router); // 重新掉uniapp上的一些有异常的方法
    registerRouter(Router, vueRouter, CONFIG.vueRouterDev);
};
export default init;