qq_44694633 2025-11-27 18:00 采纳率: 11.1%
浏览 4

iframe与目标页面内容不同步

在vue项目(端口号localhost:8081)中使用iframe嵌入目标页面(端口号localhost:5177),启动目标页面项目后,再启动使用iframe的项目,发现iframe里的页面内容与目标页面内容不同,如下图所示:
这是目标页面,注意看“体积”,和当前数量的值:

img


这是使用了iframe的页面内容:

img

这是目标页面代码:

<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的页面的代码:

<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>

我想实现:保证iframe内页面内容与目标页面内容同步。(比如在iframe里修改“当前数量”的值,对应的目标页面也要变)

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-11-27 18:12
    关注

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

    解决方案

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

    1. 禁用 iframe 缓存:在 iframe 的 src 属性中添加 ?_cacheBust=123(数字可以随意更改),例如:
    <iframe src="http://localhost:5177?_cacheBust=123" frameborder="0" width="100%" height="500"></iframe>
    
    1. 使用 iframe 的 srcdoc 属性:如果你控制目标页面的内容,可以尝试使用 iframe 的 srcdoc 属性来直接设置 iframe 内容。
    <iframe srcdoc="<html>目标页面内容</html>" frameborder="0" width="100%" height="500"></iframe>
    
    1. 使用 vue-routermode 选项:如果你使用 vue-router,尝试设置 mode 选项为 historyhash,例如:
    const router = new VueRouter({
      mode: 'history',
      routes: [...]
    })
    
    1. 检查 iframe 的 src 属性:确保 iframe 的 src 属性正确指向目标页面的 URL。
    2. 检查目标页面的 CORS 配置:如果目标页面使用了 CORS(Cross-Origin Resource Sharing),确保它允许从 iframe 的源域访问。

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

    评论

报告相同问题?

问题事件

  • 创建了问题 11月27日