Vue3 如何给 iframe src 赋值

1.定义一个变量

2.将变量赋值给 :src 

const token=localStorage.get('token');

const ifURL="http://localhost:8085/jmreport/list?token="+token;

</script>

<template>

  <iframe :src="ifURL"  frameborder="0" width="100%" :style="{height:calHeight}" scrolling="auto"></iframe>

</template>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue动态iframe 元素的 src 属性赋值可以通过以下几种方式实现: 1. 使用 Vue 的数据绑定 在 Vue 组件的 template 中使用数据绑定的方式,将 iframesrc 属性与组件中的某个变量绑定起来,当变量的值发生变化时,iframesrc 属性也会相应地更新。 例如: ``` <template> <div> <iframe :src="url"></iframe> </div> </template> <script> export default { data() { return { url: 'http://www.example.com' } }, methods: { changeUrl() { this.url = 'http://www.google.com' } } } </script> ``` 2. 使用 ref 获取 iframe 元素 在 Vue 组件中使用 ref 获取 iframe 元素,然后通过 JavaScript 动态修改 iframesrc 属性。 例如: ``` <template> <div> <iframe ref="myiframe"></iframe> </div> </template> <script> export default { mounted() { this.$refs.myiframe.src = 'http://www.example.com' }, methods: { changeUrl() { this.$refs.myiframe.src = 'http://www.google.com' } } } </script> ``` 注意:使用 ref 获取元素需要在 mounted 钩子函数中,因为只有在挂载后才能获取到元素。 3. 使用 v-if 和 v-bind:key 动态创建和销毁 iframe 元素 在 Vue 组件中使用 v-if 和 v-bind:key 动态创建和销毁 iframe 元素,并根据数据来动态设置 iframesrc 属性。 例如: ``` <template> <div> <iframe v-if="show" :src="url" :key="url"></iframe> </div> </template> <script> export default { data() { return { show: true, url: 'http://www.example.com' } }, methods: { changeUrl() { this.url = 'http://www.google.com' } } } </script> ``` 当 show 值为 true 时,会动态创建一个带有指定 src 属性的 iframe 元素;当 show 值为 false 时,会销毁 iframe 元素。当 url 值发生变化时,根据 v-bind:key 的值重新创建 iframe 元素并更新 src 属性。 以上就是在 Vue动态iframe 元素赋值的几种方法,可以根据具体的需求选择适合的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值