mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
* feat(cli): export action functions * fix: listenToSyncPath * fix: config.site.simulatorUrl
46 lines
908 B
JavaScript
46 lines
908 B
JavaScript
/**
|
|
* 同步父窗口和 iframe 的 vue-router 状态
|
|
*/
|
|
|
|
import { iframeReady } from '.';
|
|
|
|
function getCurrentDir() {
|
|
const router = window.vueRouter;
|
|
return router.currentRoute.value.path;
|
|
}
|
|
|
|
export function syncPathToParent() {
|
|
window.top.postMessage(
|
|
{
|
|
type: 'replacePath',
|
|
value: getCurrentDir(),
|
|
},
|
|
'*'
|
|
);
|
|
}
|
|
|
|
export function syncPathToChild() {
|
|
const iframe = document.querySelector('iframe');
|
|
if (iframe) {
|
|
iframeReady(iframe, () => {
|
|
iframe.postMessage(
|
|
{
|
|
type: 'replacePath',
|
|
value: getCurrentDir(),
|
|
},
|
|
'*'
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
export function listenToSyncPath(router) {
|
|
window.addEventListener('message', (event) => {
|
|
const path = event.data || '';
|
|
// should preserve hash for anchor
|
|
if (router.currentRoute.value.path !== path) {
|
|
router.replace(path).catch(() => {});
|
|
}
|
|
});
|
|
}
|