Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

持有另一个 window 中的 Vue 实例, 会导致某些情况下组件失去响应 #103

Open
yubaoquan opened this issue Dec 14, 2023 · 0 comments

Comments

@yubaoquan
Copy link
Owner

背景

一个页面中通过 iframe 加载了另一个页面, iframe 中的 vue 组件实例将自己注册到了 window 上, 并将对应的 key 值通过 postMessage 发给了父窗口. 父窗口通过 收到的 key 值将 iframe 中的子组件引用到自身的某个对象的某个值上

父组件:

window.addEventListener('message', (msg) => {
  if (msg.data?.type === 'xxx') {
    this.childRef = this.iframe.contentWindow[msg.data.id]
    this.childRef.setParent(this)
  }
})

子组件:

{
  data() {
    return {
      parent: null
    }
  },
  methods: {
    setParent(p) {
      this.parent = p
    }
  }
}

不要这样双方互相持有对方的引用!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant