解决问题:避免回退跳转其他页面回退当前页面后活跃状态的数据丢失
场景:A页面内有一个tab栏,活跃状态为tab2,跳转至B页面后回退至A页面,希望保存tab栏的活跃状态为tab2
noRefreshChangeHashQuery(queryName, value) {
const hash = location.hash;
const urlFront = location.href.replace(new RegExp(lodash.escapeRegExp(location.hash)), '');
let hashPath = '';
let hashQuery = '';
const hashQueryIndex = hash.indexOf('?');
if (hashQueryIndex > -1) {
hashPath = hash.slice(0, hashQueryIndex);
hashQuery = hash.slice(hashQueryIndex);
} else {
hashPath = hash;
}
(function changeHashQuery() {
if (hashQuery) {
const control = new URLSearchParams(hashQuery);
control.set(queryName, value);
hashQuery = `?${decodeURIComponent(control.toString())}`;
} else {
hashQuery = `?${queryName}=${value}`;
}
})();
history.replaceState(null, document.title, urlFront + hashPath + hashQuery);
}