1
0
mirror of https://gitee.com/vant-contrib/vant.git synced 2025-04-06 03:57:59 +08:00
2019-11-21 14:43:22 +08:00

30 lines
725 B
JavaScript

/**
* 同步父窗口和 iframe 的 vue-router 状态
*/
import { iframeReady, isMobile } from '.';
window.syncPath = function () {
const router = window.vueRouter;
const isInIframe = window !== window.top;
const currentDir = router.history.current.path;
if (isInIframe) {
window.top.changePath(currentDir);
} else if (!isMobile) {
const iframe = document.querySelector('iframe');
if (iframe) {
iframeReady(iframe, () => {
iframe.contentWindow.changePath(currentDir);
});
}
}
};
window.changePath = function (path = '') {
// should preserve hash for anchor
if (window.vueRouter.currentRoute.path !== path) {
window.vueRouter.replace(path).catch(() => {});
}
};