Last active
January 7, 2018 09:20
-
-
Save glacjay/3c320321b1709454fdb7e06e26b0132c to your computer and use it in GitHub Desktop.
vue 集成 iframe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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