mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-05 19:41:42 +08:00
41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
/**
|
|
* 同步父窗口和 iframe 的 vue-router 状态
|
|
*/
|
|
|
|
import { setLang } from './lang';
|
|
import { iframeReady, isMobile } from '.';
|
|
|
|
export function initIframeRouter() {
|
|
window.syncPath = function () {
|
|
const router = window.vueRouter;
|
|
const isInIframe = window !== window.top;
|
|
const currentDir = router.history.current.path;
|
|
const pathParts = currentDir.split('/');
|
|
let lang = pathParts[0];
|
|
if (currentDir[0] === '/') {
|
|
lang = pathParts[1];
|
|
}
|
|
|
|
if (!isInIframe && !isMobile) {
|
|
const iframe = document.querySelector('iframe');
|
|
if (iframe) {
|
|
iframeReady(iframe, () => {
|
|
iframe.contentWindow.changePath(lang, currentDir);
|
|
});
|
|
}
|
|
setLang(lang);
|
|
} else if (isInIframe) {
|
|
window.top.changePath(lang, currentDir);
|
|
}
|
|
};
|
|
|
|
window.changePath = function (lang, path = '') {
|
|
setLang(lang);
|
|
|
|
// should preserve hash for anchor
|
|
if (window.vueRouter.currentRoute.path !== path) {
|
|
window.vueRouter.replace(path).catch(() => {});
|
|
}
|
|
};
|
|
}
|