Skip to content

Instantly share code, notes, and snippets.

@glacjay
Last active January 7, 2018 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glacjay/3c320321b1709454fdb7e06e26b0132c to your computer and use it in GitHub Desktop.
Save glacjay/3c320321b1709454fdb7e06e26b0132c to your computer and use it in GitHub Desktop.
vue 集成 iframe
<template>
<iframe :src="the_url" @load="onIframeLoad" />
</template>
<script>
export default {
data () {
return {
the_url: "http://xxx.com/yyy",
some_id: 42
}
}
methods: {
onIframeLoad (event) {
const iframe = event.target
window.addEventListener('message', this.handleMessage)
iframe.contentWindow.eval(`
$('.some-element').attr('id', ${this.some_id})
$('.title').click(function () {
window.parent.postMessage({
cmd: 'xxx',
params: {
yyy: $('.title').attr('name')
}
})
})
`)
}
handleMessage (event) {
const data = event.data
switch (data.cmd) {
case 'xxx':
// do something
break
}
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment