mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(cli): failed to sync route (#8498)
This commit is contained in:
parent
5fe263b601
commit
3ddfe3bbb4
@ -2,7 +2,28 @@
|
|||||||
* 同步父窗口和 iframe 的 vue-router 状态
|
* 同步父窗口和 iframe 的 vue-router 状态
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { iframeReady } from '.';
|
let queue = [];
|
||||||
|
let isIframeReady = false;
|
||||||
|
|
||||||
|
function iframeReady(callback) {
|
||||||
|
if (isIframeReady) {
|
||||||
|
callback();
|
||||||
|
} else {
|
||||||
|
queue.push(callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.top === window) {
|
||||||
|
window.addEventListener('message', (event) => {
|
||||||
|
if (event.data.type === 'iframeReady') {
|
||||||
|
isIframeReady = true;
|
||||||
|
queue.forEach((callback) => callback());
|
||||||
|
queue = [];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
window.top.postMessage({ type: 'iframeReady' }, '*');
|
||||||
|
}
|
||||||
|
|
||||||
function getCurrentDir() {
|
function getCurrentDir() {
|
||||||
const router = window.vueRouter;
|
const router = window.vueRouter;
|
||||||
@ -22,8 +43,8 @@ export function syncPathToParent() {
|
|||||||
export function syncPathToChild() {
|
export function syncPathToChild() {
|
||||||
const iframe = document.querySelector('iframe');
|
const iframe = document.querySelector('iframe');
|
||||||
if (iframe) {
|
if (iframe) {
|
||||||
iframeReady(iframe, () => {
|
iframeReady(() => {
|
||||||
iframe.postMessage(
|
iframe.contentWindow.postMessage(
|
||||||
{
|
{
|
||||||
type: 'replacePath',
|
type: 'replacePath',
|
||||||
value: getCurrentDir(),
|
value: getCurrentDir(),
|
||||||
@ -36,7 +57,11 @@ export function syncPathToChild() {
|
|||||||
|
|
||||||
export function listenToSyncPath(router) {
|
export function listenToSyncPath(router) {
|
||||||
window.addEventListener('message', (event) => {
|
window.addEventListener('message', (event) => {
|
||||||
const path = event.data || '';
|
if (event.data?.type !== 'replacePath') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = event.data?.value || '';
|
||||||
// should preserve hash for anchor
|
// should preserve hash for anchor
|
||||||
if (router.currentRoute.value.path !== path) {
|
if (router.currentRoute.value.path !== path) {
|
||||||
router.replace(path).catch(() => {});
|
router.replace(path).catch(() => {});
|
||||||
|
@ -1,20 +1,3 @@
|
|||||||
function iframeReady(iframe, callback) {
|
|
||||||
const doc = iframe.contentDocument || iframe.contentWindow.document;
|
|
||||||
const interval = () => {
|
|
||||||
if (iframe.contentWindow.replacePath) {
|
|
||||||
callback();
|
|
||||||
} else {
|
|
||||||
setTimeout(interval, 50);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (doc.readyState === 'complete') {
|
|
||||||
interval();
|
|
||||||
} else {
|
|
||||||
iframe.onload = interval;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const ua = navigator.userAgent.toLowerCase();
|
const ua = navigator.userAgent.toLowerCase();
|
||||||
const isMobile = /ios|iphone|ipod|ipad|android/.test(ua);
|
const isMobile = /ios|iphone|ipod|ipad|android/.test(ua);
|
||||||
|
|
||||||
@ -25,4 +8,4 @@ export function decamelize(str, sep = '-') {
|
|||||||
.toLowerCase();
|
.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
export { isMobile, iframeReady };
|
export { isMobile };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user