vue项目使用 iframe 父子页面传值。

1 篇文章 0 订阅


一、vue项目调用iframe子页面中的方法。

1. 父页面

调用子页面方法的关键在于获取到iframe元素对象后的contentWindow属性
this.$refs.iframeName.contentWindow.FunctionName()

<template>
  <div class="pjpccx">
    <h1 class="title_color">{{ title }}</h1>
    <button @click="reportPrint">点击调用iframe中的方法</button>
    <iframe ref="iframe" src="/static/zkjs/base.html" width="100%" height="100%" style="border: none;" class="iframe" frameborder="0" scrolling="yes" name="iframe" seamless>您的浏览器版本过低,无法显示报表内容!建议升级浏览器或更换浏览器!</iframe>
  </div>
</template>

export default {
	methods: {
    reportPrint() {
      // 父页面调用子页面方法
      this.$refs.iframe.contentWindow.openThePage()
      // 父页面向子页面传值
      this.$refs.iframe.contentWindow.postMessage(param)
    }
  }
}

2. 子页面

  <script type="text/javascript">
    // 打开某一页
    function openThePage() {
      new Device().startFun()
    }
    // 子页面接受父页面的传值
    window.addEventListener('message', messageEvent => {
      if (messageEvent.source != window.parent.window.parent) return;
      paramData = messageEvent.data
      changeUrl(paramData)
    })
  </script>

3. 子页面监听不到父页面的情况

父页面用this.$refs.iframe.contentWindow.postMessage(param),子页面监听不到父页面的传值的情况使用 onload

export default {
  methods: {
    reportPrint () {
      const param = {
        name: 'name',
        value: '父传给子的值',
      }
      // 子页面监听不到父页面的情况使用以下方式
      const myFrame = this.$refs['iframe'];
      myFrame.onload = function () {
        const iframeWin = myFrame.contentWindow
        iframeWin.postMessage(param, '*')
      }
    }
  }
}

二、在iframe页面中,调用引用它的父页面中的方法。

1. 父页面

父页面需要添加事件监听器,在回调中执行需要执行方法或者使用参数,在组件生命周期结束的时候销毁监听器

export default {
  mounted() {
    // 添加事件监听器
    window.addEventListener('message', this.handleMessage);
    // 第二种
    let _this = this
    window.addEventListener('message', function (e) {
      console.log(e, e.data.Name||e.data.result.Name)
      _this.form.name = e.data.Name||e.data.result.Name
    })
  },
  beforeDestroy() { // 移除事件监听器
    window.removeEventListener('message', this.handleMessage);
  },
  methods: {
    handleMessage(e) {
      console.log(e.data.Name,e.data.result.Name)
    }
  }
}

2. 子页面

子页面通过window.parent.postMessage方法实现的通信

function setCertificateData(result) {
  // 子页面传值给父页面
  window.parent.postMessage({
    Name: "张三",
  }, '*');
  // 子页面传值给指定父页面
  window.parent.postMessage({Name: "张三"}, 'http://localhost:8080/#/formVue');
  window.parent.postMessage({result: {Name: "张三"}}, 'http://localhost:8080/#/formVue');
 }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3中,父子组件通信有以下三种方式: 1. 组件给子组件传参: 组件中通过props向子组件传递数据,子组件中通过props接收数据。 2. 子组件调用组件的方法: 组件中通过v-on绑定一个方法,子组件中通过$emit触发这个方法,并传递数据。 3. 子组件暴露属性给组件: 子组件中通过defineExpose暴露属性或方法,组件中通过ref获取子组件实例,再调用子组件的属性或方法。 以下是三种方式的代码示例: 1. 组件给子组件传参: 组件中: ```vue <template> <child-component :message="msg"></child-component> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, data() { return { msg: 'Hello World', }; }, }; </script> ``` 子组件中: ```vue <template> <div>{{ message }}</div> </template> <script> export default { props: { message: String, },}; </script> ``` 2. 子组件调用组件的方法: 组件中: ```vue <template> <child-component @change-visible="handleChangeVisible"></child-component> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, methods: { handleChangeVisible(visible) { console.log(visible); }, }, }; </script> ``` 子组件中: ```vue <template> <button @click="handleClick">Change Visible</button> </template> <script> export default { methods: { handleClick() { this.$emit('change-visible', false); }, }, }; </script> ``` 3. 子组件暴露属性给组件: 子组件中: ```vue <template> <div>{{ message }}</div> </template> <script> import { defineExpose } from 'vue'; export default { setup() { const message = 'Hello World'; defineExpose({ message, }); return { message, }; }, }; </script> ``` 组件中: ```vue <template> <child-component ref="child"></child-component> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent, }, mounted() { console.log(this.$refs.child.message); }, }; </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值