今天看到一个同事研究给iframe传参,由于好奇,我自己也写了个demo,说起来其实也挺简单的,但是在此之前没有用过,便想记录一下

其中主要用到的是postMessage

在页面中引入一个iframe标签

<template>
<div>
<iframe src="http://10.36.37.40:8081" ref="iframe"></iframe>
     <input v-module='inputVal'>
      <button @click='sendMessage'></button>
</div>
</template>

  在父页面添加监听iframe传过来的数据,并且向iframe传参

<script>
export default {
data: {
iframeWin: null
},
     mounted(){
        window.addEventListener('message', this.handleMessage) // 监听iframe的事件
        this.iframeWin = this.$refs.iframe.contentWindow
     },
     methods: {
       handleMessage(event) {
        let data=event.data
      },
      sendMessage() {
// 给iframe传参
this.iframeWin.postMessage({
          cmd: 'getParams', // cmd 用来判断触发的是什么事件
          params: {
            key: this.params,
          }
          }, '*')
      }
     }
}

在iframe中向页面传递信息

<template>
<div>{{params}}</div>
</template>
<script>
  export default {
    data: {
      params: null
    },
    mounted() {
      // 接受父页面发来的信息
      window.addEventListener('message', (event) => {
      console.log(event)
      let data = event.data
      console.log(data)
      switch (data.cmd) {
      case 'getParams':
     console.log('public data', data)
     this.params = data.params.key
      }
      })
    }
  }
</script>

这样就可以实现将页面中输入的内容同步到iframe中了

h5嵌套iframe实时传参(适用vue)的更多相关文章

  1. 【vue】父向子组件传参、子组件向父传参

    1.父向子组件传参 App.vue为父,引入componetA组件之后,则可以在App.vue中使用标签(注意驼峰写法要改成componet-a写法,因为html对大小写不敏感,componenta与 ...

  2. vue,一路走来(12)--父与子之间传参

    今天想起一直没有记录父组件与子组件的传参问题,这在项目中一直用到. 父向子组件传参 Index.vue父组件中 <component-a :msgfromfa="(positionno ...

  3. Vue的Router路由传参

    一.文件结构 二.vue.js 打开此链接 https://cdn.bootcss.com/vue/2.6.10/vue.js 复制粘贴页面的所有内容 三.vue-router.js 打开此链接  h ...

  4. vue路由(一个包含重定向、嵌套路由、懒加载的main.js如下)and 路由跳转传参的query和params的异同

    import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)cons ...

  5. Vue学习之路由vue-router传参及嵌套小结(十)

    一.路由传递参数: 1.使用query传值: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  6. 帆软用工具测试超链接打开弹窗(iframe嵌套),解决js传参带中文传递有乱码问题

    1.新建超链接 随意点击一个单元格右击,选择 超级链接 2.在弹出的窗口中选择JavaScript脚本 如图: 其中红框框出的是几个要点   ,左边的就不讲了,右上角的参数cc是设置了公式remote ...

  7. vue页面传参和接参

    https://blog.csdn.net/zhouzuoluo/article/details/81259298(copy) js** this.$router.push({ name: 'Flow ...

  8. vue2.0路由写法、传参和嵌套

    前置知识请戳这里 vue-routerCDN地址:https://unpkg.com/vue-router@3.0.1/dist/vue-router.js vue-router下载地址:https: ...

  9. 从 Vue 的视角学 React(四)—— 组件传参

    组件化开发的时候,参数传递是非常关键的环节 哪些参数放在组件内部管理,哪些参数由父组件传入,哪些状态需要反馈给父组件,都需要在设计组件的时候想清楚 但实现这些交互的基础,是明白组件之间参数传递的方式, ...

随机推荐

  1. kotlin 泛型中类型投射

    fun main(arg: Array<String>) { var ints:Array<Int> = arrayOf(, , ) val any =Array<Any ...

  2. php+mysql模糊查询功能

    一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表示任意0个或多个字符.可匹配任意类型和长度的字符,有 ...

  3. 003-多线程-JUC线程池-几种特殊的ThreadPoolExecutor【newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor、newScheduledThreadPool】

    一.概述 在java doc中,并不提倡我们直接使用ThreadPoolExecutor,而是使用Executors类中提供的几个静态方法来创建线程池: 以下方法是Executors下的静态方法,Ex ...

  4. test20190510

  5. .Netcore 2.0 Ocelot Api网关教程(9)- QoS

    本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断. 1.添加Nuget包 添加 Ocelot.Provider.Polly  ...

  6. ssl证书泛域名

    certbot certonly -d *.example.com --manual --preferred-challenges dns --server https://acme-v02.api. ...

  7. flask上下管理文相关 - RequestContetxt & AppContext对象

    RequestContetxt / AppContext对象 1.flask程序启动时 当flask程序启动时,通过源码可知,首先会创建两个LocalStack对象,二者内部会分别创建Local对象( ...

  8. ADRMS与office的整合(一)

    因为微软之前针对客户的RMS加密服务是一种免费的测试服务,虽然用户很多但实质上还是一种“测试服务”. 后来微软把这个服务商业化了,需要继续使用的话需要打下这个补丁 https://support.mi ...

  9. C/C++.字符串分割

    1.ZC:只测试使用了 自己改编的函数SplitStr_ZZ(...),其它的 未测试 #include <string> #include <vector> #include ...

  10. centos7 64位如何配置网络

    在虚拟机的操作的时候,修改  ifcfg-eno16777736 可能没有权限 su - //进入root用户状态chmod a+w  ifcfg-eno16777736//把该文件修改为可写状态 我 ...