vue-router 内嵌 iframe,导致this.$router.go(-1)返回上一页异常

问题描述:

在使用vue-router跳转过来的页面中内嵌了一个iframe,通过选择不同项替换iframe的src,但是在使用this.$router.go(-1)返回之前的页面时发现页面刷新,点击多次才能返回之前的页面

原因:

浏览器机制的原因,在 iframe 导航变化后手动点击浏览器的后退按钮也依然只是后退 iframe 中的导航。类似window.history.go

解决方法:

iframe 里面的链接用 location.replace 跳转,这样就只会有一个历史记录了

<iframe ref="iframe"></iframe>
this.$refs.iframe.contentWindow.location.replace(src)

 

你可以将router-view替换为iframe来显示外链页面。可以通过以下步骤来实现: 1. 在使用vue-router的时候,确保你已经安装了vue-router,并且在main.js中正确引入了Vue、App和router。 2.router中定义你的路由配置,包括内嵌的组件和外链的URL。 3. 在App.vue中将router-view替换为iframe标签,并通过watch监听路由的变化。 4. 在watch的回调函数中,使用.slice(0, 4)来判断是否是外链URL。如果是外链URL,则使用iframe来显示;如果不是外链URL,则继续使用router-view来显示组件。 以下是一个示例代码片段,展示了如何将router-view替换为iframe: ```javascript // main.js import Vue from 'vue'; import App from './App'; import router from './router'; new Vue({ router, render: h => h(App) }).$mount('#app'); // App.vue <template> <div id="app"> <iframe v-if="isExternalLink" :src="currentRoute"></iframe> <router-view v-else></router-view> </div> </template> <script> export default { data() { return { currentRoute: '', isExternalLink: false }; }, watch: { $route(to) { this.currentRoute = to.path; this.isExternalLink = this.currentRoute.slice(0, 4) === 'http'; } } }; </script> ``` 请注意,以上代码仅作为示例,具体实现还需要根据你的项目结构和需求进行适当调整。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [vue-routerrouter-view不能渲染的解决方法](https://download.csdn.net/download/weixin_38663608/13582607)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3 二级路由-内嵌外链解决方案(iframe)](https://blog.csdn.net/qq_28805447/article/details/127566199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值