vue使用iframe的方法和数据通信
使用
将需要引入的
html和js等放在项目根目录下的static文件夹下引入
iframe1
<iframe :src="iframeSrc" frameborder="0" width="660" height="600" ref="iframe"></iframe>
1
iframeSrc: '/assets/previewFile.html'
注意使用绝对路径,且路径为打包后的二级目录。事实上是用
webpack插件将static目录下的文件复制到打包后的二级目录。分别在
webpack.dev.conf.js和webpack.prod.conf.js里配置插件1
2
3
4
5new CopyWebpackPlugin([{
from: path.resolve(__dirname, '../static'),
to: config.dev.assetsSubDirectory,
ignore: ['.*']
}])
通信
在父中,使用postMessage发消息
1 | this.$refs.iframe.contentWindow.postMessage( |
子(html)中,接收数据
1 | let previewUrl=''; |