wujingjing
2025-03-25 b371823cede83f4134f5fada68b161b33612f871
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * 设置全局函数,用于 Child 注册
 */
export const registerChildFunction = () => {
    /** @description 把 Child 的函数注册过来, 方便自己调用 */
    (window as any).register = (cb: (...args: any[]) => void) => {
        console.log('register children function');
        (window as any).childCall = (...args: any[]) => {
            cb(...args);
        };
    };
};
 
/**
 * 往 windows 上加函数,给 child 调用
 */
export const setParentFunction = () => {
    const child = window.frames[0];
    if (!child) return;
    const fun = (msg: string) => {
        console.log('函数来自parent', msg);
    };
    (child as any).register?.(fun);
};
 
/**
 * 子 iframe 调用
 */
export const childCall = (...args: any[]) => {
    (window.frames[0] as any)?.childCall?.(...args);
};