qq_44694633 2025-11-27 17:38 采纳率: 11.1%
浏览 1

iframe与目标页面内容不同步问题

在vue项目中使用iframe内嵌页面,结果发现内嵌的页面内容(使iframe的项目端口号localhost:8081)与真实目标页面(端口号localhost:5177)内容不同步,如下图所示:

img

img


这是使用iframe的页面的代码:

<template>
  <div id="app">
    <div>
      <el-button @click="toPage2">传送</el-button>
    </div>
    <div id="viewArea" v-if="showIframe">
      <iframe
          src="http://localhost:5177/page2"
          width="100%"
          height="100%"
          frameborder="0"
      >
      </iframe>
    </div>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'app',
  components: {
    HelloWorld
  },
  data(){
    return{
      showIframe:false,
    }
  },
  methods:{
    toPage2(){
      this.showIframe = true;
      console.log('打开了传送按钮,iframe显示了');
    },
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
#viewArea{
  width: 100%;
  height:80%;
  position: fixed;
  background-color: blue;
}
iframe{
  width: 100%;
  height: 100%;
}
</style>


这是目标页面代码:

<template>
<!-- By master update -->
<!-- 组员新建分支后,就可以自由发挥开发了,写完后需要往远程仓库的自己的分支上push,怎么操作?-->
<!--  By m1 update-->
  <h1>thisis2Page</h1>
  <button @click="toPageFirist" class="toFirst">去一号页</button>
  <br>
  <p>
    <button @click="diff">-</button> 当前数量:{{ count }}<button @click="add">+</button>
  </p>
  <!-- 添加显示接收到的消息区域 -->
  <p>reactive立方体计算器对象例子:</p><br/>
  <table>
    <tr>
      <td>长(m)</td>
      <td><input type="number" v-model.number="cube.length"/></td>
    </tr>
    <tr>
      <td>宽(m)</td>
      <td><input type="number" v-model.number="cube.width"/></td>
    </tr>
    <tr>
      <td>高(m)</td>
      <td><input type="number" v-model.number="cube.height"/></td>
    </tr>
    <tr>
      <td>体积(m³)</td>
      <td>{{ cube.volume }}</td>
    </tr>
    <tr>
      <td>
        <button @click="jisuan">计算</button>
      </td>
      <td>
        <button @click="reset">重置</button>
      </td>
    </tr>
  </table>
</template>

<script setup>
// 从vue-router中导入useRouter函数,用于获取路由实例
import { useRouter } from 'vue-router'
// 导入必要的Vue响应式API
import { ref, reactive, onMounted, onUnmounted } from 'vue'

const count = ref(0)
// 添加接收消息的响应式变量
const receivedMessage = ref('')

// 修复体积属性名拼写错误:将voulme改为volume
const cube = reactive({
  length: 0,
  width: 0,
  height: 0,
  volume: 0, // 修复拼写错误
})

function add() {
  count.value++
}

function diff() {
  count.value--
}

function jisuan() {
  // 确保计算结果正确赋值给正确的属性名
  cube.volume = cube.length * cube.width * cube.height
  console.log('计算体积:', cube.volume)
}

function reset() {
  cube.length = 0
  cube.width = 0
  cube.height = 0
  cube.volume = 0
}
const router = useRouter()
function toPageFirist() {
  router.push('/page1')
}
</script>

<style scoped>
.toFirst {
  padding: 8px 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  cursor: pointer;
}

.toFirst:hover {
  background-color: #45a049;
}

input[type="number"] {
  width: 100px;
  padding: 5px;
}

table {
  border-collapse: collapse;
  width: 300px;
  margin: 20px auto;
}

table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: center;
}

button {
  padding: 5px 10px;
  margin: 0 5px;
}
</style>


我想实现:目标页面前端服务已开启且在浏览器只开一个页面的情况下,iframe内容与目标页面一致(比如显示的“当前数量”、“体积”值相同)

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-11-27 17:51
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解决方案

    你遇到的问题可能是由于 iframe 内嵌页面的缓存导致的。尝试以下解决方案:

    1. 禁用 iframe 缓存:在 iframe 的 src 属性中添加 ?_cacheBust=123(数字可以随意更改),例如:
    <iframe src="http://localhost:8081?_cacheBust=123" frameborder="0" width="100%" height="500"></iframe>
    
    1. 使用 iframe 的 srcdoc 属性:如果你控制目标页面的内容,可以尝试使用 iframe 的 srcdoc 属性直接嵌入目标页面的 HTML 内容。
    <iframe srcdoc="<html>目标页面的HTML内容</html>" frameborder="0" width="100%" height="500"></iframe>
    
    1. 使用 iframe 的 sandbox 属性:设置 iframe 的 sandbox 属性为 "allow-scripts",允许 iframe 内嵌页面执行脚本。
    <iframe src="http://localhost:8081" frameborder="0" width="100%" height="500" sandbox="allow-scripts"></iframe>
    
    1. 检查目标页面的内容是否正确:确保目标页面的内容正确无误,否则 iframe 内嵌页面将显示错误的内容。

    如果以上解决方案仍然无法解决问题,请提供更多详细信息或代码片段,我将继续帮助你解决问题。

    评论

报告相同问题?

问题事件

  • 创建了问题 11月27日