init
This commit is contained in:
76
node_modules/uni-simple-router/src/H5/buildRouter.ts
generated
vendored
Normal file
76
node_modules/uni-simple-router/src/H5/buildRouter.ts
generated
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
import {RoutesRule, Router, routesMapRule, totalNextRoute, hookToggle, navtoRule} from '../options/base';
|
||||
import {H5Config} from '../options/config';
|
||||
import {warn} from '../helpers/warn'
|
||||
import {getDataType, getRoutePath} from '../helpers/utils'
|
||||
import { onTriggerEachHook } from '../public/hooks';
|
||||
|
||||
export function buildVueRoutes(router: Router, vueRouteMap:RoutesRule):RoutesRule {
|
||||
const {pathMap, finallyPathList} = (router.routesMap as routesMapRule);
|
||||
const vueRoutePathList:Array<string> = Object.keys(vueRouteMap);
|
||||
for (let i = 0; i < vueRoutePathList.length; i++) {
|
||||
const path = vueRoutePathList[i];
|
||||
const myRoute:RoutesRule = pathMap[path];
|
||||
const vueRoute:RoutesRule = vueRouteMap[path];
|
||||
if (!myRoute) {
|
||||
warn(`${path} 路由地址在路由表中未找到,确定是否传递漏啦`, router, true);
|
||||
} else {
|
||||
const {finallyPath} = getRoutePath(myRoute, router);
|
||||
if (finallyPath instanceof Array) {
|
||||
throw new Error(`非 vueRouterDev 模式下,alias、aliasPath、path 无法提供数组类型! ${JSON.stringify(myRoute)}`);
|
||||
}
|
||||
if (myRoute.name != null) {
|
||||
vueRoute.name = myRoute.name;
|
||||
}
|
||||
const vuePath = vueRoute['path'];
|
||||
const vueAlias = vueRoute['alias'];
|
||||
delete vueRoute['alias'];
|
||||
vueRoute['path'] = (finallyPath as string);
|
||||
if (vuePath === '/' && vueAlias != null) {
|
||||
vueRoute['alias'] = vueAlias;
|
||||
vueRoute['path'] = vuePath;
|
||||
}
|
||||
const beforeEnter = myRoute.beforeEnter;
|
||||
if (beforeEnter) {
|
||||
vueRoute['beforeEnter'] = function(
|
||||
to:totalNextRoute,
|
||||
from: totalNextRoute,
|
||||
next:(rule?: navtoRule|false)=>void,
|
||||
):void{
|
||||
onTriggerEachHook(to, from, router, hookToggle['enterHooks'], next)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
if (finallyPathList.includes('*')) {
|
||||
vueRouteMap['*'] = pathMap['*']
|
||||
}
|
||||
return vueRouteMap
|
||||
}
|
||||
|
||||
export function buildVueRouter(router:Router, vueRouter:any, vueRouteMap:RoutesRule|RoutesRule[]) :void |never {
|
||||
let routes:RoutesRule[] = [];
|
||||
if (getDataType<RoutesRule|RoutesRule[]>(vueRouteMap) === '[object Array]') {
|
||||
routes = (vueRouteMap as RoutesRule[]);
|
||||
} else {
|
||||
routes = Object.values(vueRouteMap);
|
||||
}
|
||||
const {scrollBehavior, fallback} = router.options.h5 as H5Config;
|
||||
const oldScrollBehavior = vueRouter.options.scrollBehavior;
|
||||
vueRouter.options.scrollBehavior = function proxyScrollBehavior(
|
||||
to:totalNextRoute,
|
||||
from:totalNextRoute,
|
||||
savedPosition:any
|
||||
) {
|
||||
oldScrollBehavior && oldScrollBehavior(to, from, savedPosition);
|
||||
return (scrollBehavior as Function)(to, from, savedPosition)
|
||||
}
|
||||
vueRouter.fallback = fallback;
|
||||
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
|
||||
const newVueRouter:any = new vueRouter.constructor({
|
||||
...router.options.h5,
|
||||
base: vueRouter.options.base,
|
||||
mode: vueRouter.options.mode,
|
||||
routes
|
||||
});
|
||||
vueRouter.matcher = newVueRouter.matcher;
|
||||
}
|
||||
71
node_modules/uni-simple-router/src/H5/proxyHook.ts
generated
vendored
Normal file
71
node_modules/uni-simple-router/src/H5/proxyHook.ts
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
import {Router, proxyHookName, totalNextRoute, navtoRule} from '../options/base';
|
||||
|
||||
export class MyArray extends Array {
|
||||
constructor(
|
||||
private router:Router,
|
||||
private vueEachArray:Array<Function>,
|
||||
private myEachHook:Function,
|
||||
private hookName:'beforeHooks'| 'afterHooks',
|
||||
) {
|
||||
super();
|
||||
Object.setPrototypeOf(this, MyArray.prototype)
|
||||
}
|
||||
push(v:any):any {
|
||||
this.vueEachArray.push(v);
|
||||
const index = this.length;
|
||||
this[this.length] = (to: totalNextRoute, from: totalNextRoute, next:(rule?: navtoRule|false)=>void) => {
|
||||
if (index > 0) {
|
||||
this.vueEachArray[index](to, from, () => {
|
||||
next && next()
|
||||
});
|
||||
} else {
|
||||
this.myEachHook(to, from, (nextTo?:navtoRule|false) => {
|
||||
// Fixe https://github.com/SilurianYang/uni-simple-router/issues/241 2021年3月6日22:15:27
|
||||
// 目前不调用uni-app的守卫函数,因为会丢失页面栈信息
|
||||
if (nextTo === false) {
|
||||
next(false);
|
||||
} else {
|
||||
this.vueEachArray[index](to, from, (uniNextTo?:navtoRule|false) => {
|
||||
next(nextTo);
|
||||
})
|
||||
}
|
||||
}, this.router, true);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function proxyEachHook(router:Router, vueRouter:any):void {
|
||||
const hookList:Array<'beforeHooks'| 'afterHooks'> = ['beforeHooks', 'afterHooks'];
|
||||
for (let i = 0; i < hookList.length; i++) {
|
||||
const hookName = hookList[i];
|
||||
const myEachHook = router.lifeCycle[(hookName as proxyHookName)][0];
|
||||
if (myEachHook) {
|
||||
const vueEachArray:Array<Function> = vueRouter[hookName];
|
||||
vueRouter[hookName] = new MyArray(router, vueEachArray, myEachHook, hookName);
|
||||
}
|
||||
}
|
||||
}
|
||||
export function proxyH5Mount(router:Router):void {
|
||||
if (router.mount.length === 0) {
|
||||
if (router.options.h5?.vueRouterDev) {
|
||||
return
|
||||
}
|
||||
const uAgent = navigator.userAgent;
|
||||
const isIos = !!uAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
|
||||
if (isIos) {
|
||||
// 【Fixe】 https://github.com/SilurianYang/uni-simple-router/issues/109
|
||||
setTimeout(() => {
|
||||
const element = document.getElementsByTagName('uni-page');
|
||||
if (element.length > 0) {
|
||||
return false
|
||||
}
|
||||
window.location.reload();
|
||||
}, 0);
|
||||
}
|
||||
} else {
|
||||
const [{app}] = router.mount;
|
||||
app.$mount();
|
||||
router.mount = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user