| | |
| | | (window as any).register = (cb: (...args: any[]) => void) => { |
| | | console.log('register children function'); |
| | | (window as any).childCall = (...args: any[]) => { |
| | | cb(...args); |
| | | cb?.(...args); |
| | | }; |
| | | }; |
| | | }; |
| | | |
| | | const fun = (msg: string) => { |
| | | console.log('函数来自parent', msg); |
| | | }; |
| | | /** |
| | | * 往 windows 上加函数,给 child 调用 |
| | | */ |
| | | export const setParentFunction = () => { |
| | | const child = window.frames[0]; |
| | | let child: any; |
| | | for (let index = 0; index < window.frames.length; index++) { |
| | | const frame = window.frames[index]; |
| | | if ((frame as any).register) { |
| | | child = frame; |
| | | break; |
| | | } |
| | | } |
| | | if (!child) return; |
| | | const fun = (msg: string) => { |
| | | console.log('函数来自parent', msg); |
| | | }; |
| | | (child as any).register?.(fun); |
| | | |
| | | child.register?.(fun); |
| | | }; |
| | | |
| | | /** |
| | | * 子 iframe 调用 |
| | | */ |
| | | export const childCall = (...args: any[]) => { |
| | | (window.frames[0] as any)?.childCall?.(...args); |
| | | (window as any)?.childCall?.(...args); |
| | | }; |
| | | |
| | | for (let index = 0; index < window.frames.length; index++) { |
| | | const element = window.frames[index]; |
| | | console.log('🚀 ~ element:', element); |
| | | } |