首页 > 其他分享 >在 Vue 中使用 iframe 嵌套页面

在 Vue 中使用 iframe 嵌套页面

时间:2023-05-19 09:11:07浏览次数:32  
标签:Vue 嵌套 iframe 使用 message 页面

1.在 Vue 中引入 iframe

在 Vue 中使用 iframe 技术需要在组件中引入 iframe 标签,代码如下:

<template>
  <div>
    <iframe src="https://www.baidu.com"></iframe>
  </div>
</template>

2.设置 iframe 的样式

在 Vue 中使用 iframe 技术需要设置 iframe 的样式,包括宽度、高度、边框等。代码如下:

<template>
  <div>
    <iframe src="https://www.baidu.com" width="100%" height="500px" frameborder="0"></iframe>
  </div>
</template>

3.在 iframe 中嵌套子页面

在 Vue 中使用 iframe 技术可以嵌套子页面,代码如下:

<template>
  <div>
    <iframe src="./subpage.html" width="100%" height="500px" frameborder="0"></iframe>
  </div>
</template>

4.在子页面中设置数据传递

在 Vue 中使用 iframe 技术可以实现子页面和父页面之间的数据传递,代码如下:

在子页面中设置一个按钮,并在点击按钮时向父页面传递数据:

<template>
  <div>
    <button @click="sendMessage">发送消息</button>
  </div>
</template>

<script>
export default {
  methods: {
    sendMessage() {
      window.parent.postMessage({ message: 'Hello, parent!' }, '*')
    },
  },
}
</script>

在父页面中监听子页面传递的数据,并进行处理:

<template>
  <div>
    <iframe src="./subpage.html" width="100%" height="500px" frameborder="0" @load="loadIframe"></iframe>
  </div>
</template>

<script>
export default {
  methods: {
    loadIframe() {
      const iframe = document.querySelector('iframe')
      iframe.contentWindow.addEventListener('message', (event) => {
        console.log(event.data.message)
      })
    },
  },
}
</script>

以上就是在 Vue 中使用 iframe 技术嵌套页面的详细步骤。使用 iframe 技术可以实现多个页面之间的数据传递和交互,提高了网站的整体性能和用户体验。

 
 
 

标签:Vue,嵌套,iframe,使用,message,页面
From: https://www.cnblogs.com/lizeqian1994/p/17413917.html

相关文章

  • Vue 自定义指令实践
    Vue自定义指令一个自定义指令由一个包含类似组件生命周期钩子的对象来定义。钩子函数会接收到指令所绑定元素作为其参数。在 <scriptsetup> 中,任何以 v 开头的驼峰式命名的变量都可以被用作一个自定义指令。eg: 在上面的例子中,vFocus 即可以在模板中以 v-focus 的形......
  • Vue Ant Design Pro 中定制主题
    VueAntDesignPro中定制主题需求定制主题去除“正在切换主题”过渡效果 需求项目要求使用草绿色的主题色并且去除如下的loading效果定制主题这里可以参照AntDesignProofVue官方文档哦定制方式是使用 less 的modifyVars的方式进行覆盖变量官方文档......
  • 在Vue2项目中遇到的问题汇总
    1问题1:Proxyerror:Couldnotproxyrequest/studentsfromlocalhost:8080tohttp://localhost:5000/.SeehtProxyerror:Couldnotproxyrequest/studentsfromlocalhost:8080tohttp://localhost:5000/.Seeht终端报错:浏览器报错这个里面给的文件没有打开,打......
  • Vue3迎来升级,全面助力企业数字化转型
    近年来,随着“互联网+”的推进与应用普及,数字化转型已经成为企业发展的必备选项。低代码开发平台的迅速普及和广泛应用,也为企业数字化的转型提供了支撑技术。JNPF快速开发平台深度集成java+.net 6 双技术引擎,具备易维护、便部署、高集成、高效率等多方面特性,面向企业项目提供开......
  • VUE项目
    一、创建vue工程使用vue工程之前,确保已经安装了nodejs,在命令行中输入node-v可以看到node版本即可。nodejs官网下载地址http://nodejs.cn/download/current/安装vue#在cmd或pycharm的下方Terminal中执行命令npmconfigsetregistryhttps://registry.npm.taobao.org#......
  • vue中pdf预览,报错(Cannot read properties of undefined (reading ‘catch‘))解决
    1、在node_modules中找到vue-pdf,src下的pdfjsWrapper.jsif(pdfRender!==null){if(canceling)return;canceling=true;pdfRender.cancel().catch(function(err){emitEvent('error',err);});return;}修改为if(pdf......
  • VUE基础
    一、基础vue和jquery一样,是前端的js库在html文件中使用需导入vue.js:<scriptsrc="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>{{xx}}来表示取某个变量xx的值二、vue指令v-model:双向数据绑定用来在input、select、textarea、checkbox、radio等表单控......
  • vue中事件修饰符.prevent.self和.self.prevent有什么区别
    v-on:click.prevent.self:会阻止所有的点击,阻止了自身的默认事件和阻止了子元素事件的向上冒泡。v-on:click.self.prevent:只会阻止对元素自身的点击,阻止了子元素事件的向上冒泡,阻止了子组件冒泡事件的默认事件。在Vue.js中,事件修饰符.prevent用于阻止事件的默认行为,.self用于阻......
  • error Resolve error: @vitejs/plugin-vue requires vue (>=3.2.13) or @vue/compile
    创建Vue3项目时出错error Resolveerror:@vitejs/plugin-vuerequiresvue(>=3.2.13)or@vue/compiler-sfctobepresentinthedependencytree将vue 升级到 [email protected]  版本 执行[email protected] ......
  • Vue框架中,何为渐进式框架?
    我的理解是:vue框架提供了不同层次的特性与功能,在使用过程中丰俭由人,既可以只使用核心特性,又可以使用高级特性及第三方功能。核心:数据驱动(视图的内容随着数据的改变而改变)组件化(可以增加代码的复用性,可维护)无论再怎么灵活,Vue的核心知识在所有这些用例中都是通用的。......