无法实现多页面多路由的源码分析
createWebHistory和createWebHashHistory的区别
首先是createWebHistory
function createWebHistory(base) {
base = normalizeBase(base);
const historyNavigation = useHistoryStateNavigation(base);
const historyListeners = useHistoryListeners(base, historyNavigation.state, historyNavigation.location, historyNavigation.replace);
function go(delta, triggerListeners = true) {
if (!triggerListeners)
historyListeners.pauseListeners();
history.go(delta);
}
进入normalizeBase()方法
function normalizeBase(base) {
if (!base) {
if (isBrowser) {
const baseEl = document.querySelector('base');
base = (baseEl && baseEl.getAttribute('href')) || '/';
base = base.replace(/^\w+:\/\/[^\/]+/, '');
}
else {
base = '/';
}
}
补充一下:
表达式a && 表达式b : 计算表达式a(也可以是函数)的运算结果,
如果为 True, 执行表达式b(或函数),并返回b的结果;
如果为 False,返回a的结果;
表达式a || 表达式b : 计算表达式a(也可以是函数)的运算结果,
如果为 Fasle, 执行表达式b(或函数),并返回b的结果;
如果为 True,返回a的结果;
对象为true;
非零数字为true;
零为false;
非空字符串为true;
空字符串为法false;
其他为false;
createWebHashHistory
Params:
base – optional base to provide. Defaults to location.pathname + location.search If there is a <base> tag in the head, its value will be ignored in favor of this parameter but note it affects all the history.pushState() calls, meaning that if you use a <base> tag, it's href value has to match this parameter (ignoring anything after the #).
function createWebHashHistory(base) {
base = location.host ? base || location.pathname + location.search : '';
if (!base.includes('#'))
base += '#';
if ((process.env.NODE_ENV !== 'production') && !base.endsWith('#/') && !base.endsWith('#')) {
warn(`A hash base must end with a "#":\n"${base}" should be "${base.replace(/#.*$/, '#')}".`);
}
return createWebHistory(base);
}
个人猜测, 希望大佬多多指正!
|