当前位置: 首页 > news >正文

株洲网站建设平台凤翔做网站

株洲网站建设平台,凤翔做网站,破解wordpress,一起秀h5怎么制作最近做了个项目&#xff0c;其中有个页面是由 iframe 嵌套了一个另外的页面&#xff0c;在运行的过程中发现 KeepAlive 并不生效&#xff0c;每次切换路由都会触发 iframe 页面的重新渲染&#xff0c;代码如下&#xff1a; <router-view v-slot"{ Component }">…

最近做了个项目,其中有个页面是由 iframe 嵌套了一个另外的页面,在运行的过程中发现 KeepAlive 并不生效,每次切换路由都会触发 iframe 页面的重新渲染,代码如下:

  <router-view v-slot="{ Component }"><keep-alive :include="keepAliveList"><component :is="Component"></component></keep-alive></router-view>

看起来并没有什么问题,并且其他非 iframe 实现的页面都是可以被缓存的,因此可以推断问题出在 iframe 的实现上。

我们先了解下 KeepAlive

KeepAlive (熟悉的可跳过本节)

被 KeepAlive 包裹的组件不是真的卸载,而是从原来的容器搬运到另外一个隐藏容器中,实现“假卸载”, 当被搬运的容器需要再次挂载时,应该把组件从隐藏容器再搬运到原容器,这个过程对应到组件的生命周期就是 activated 和 deactivated

keepAlive 是需要渲染器支持的,在执行 mountComponent 时,如果发现是 __isKeepAlive 组件,那么会在上下文注入 move 方法。

function mountComponent(vnode, container, anchor) {/**... */const instance = {/** ... */state,props: shallowReactive(props),// KeepAlive 实例独有keepAliveCtx: null};const isKeepAlive = vnode.__isKeepAlive;if (isKeepAlive) {instance.keepAliveCtx = {move(vnode, container, anchor) {insert(vnode.component.subTree.el, container, anchor);},createElement};}
}

原因

通过上面的了解,我们知道,KeepAlive 缓存的是 vnode 节点,vnode 上面会有对应的真实DOM。组件“销毁”时,会将真实 DOM 移动到“隐藏容器”中,组件重新“渲染”时会从 vnode 上取到真实 DOM,再重新插入到页面中。这样对普通元素是没有影响的,但是 iframe 很特别,当其插入到页面时会重新加载,这是浏览器特性,与 Vue 无关。

解决方案

思路:路由第一次加载时将 iframe 渲染到页面中,路由切换时通过 v-show 改变显/隐。

  1. 在路由注册时,将 component 赋值为一个空组件
      {path: "/chathub",name: "chathub",component: { render() {} }, // 这里写 null 时控制台会出 warning,提示缺少 render 函数},
    
  2. 在 router-view 处,渲染 iframe,通过 v-show 来控制显示隐藏
      <ChatHub v-if="chatHubVisited" v-show="isChatHubPage"></ChatHub><router-view v-slot="{ Component }"><keep-alive :include="keepAliveList"><component :is="Component"></component></keep-alive></router-view>
    
  3. 监听路由的变化,改变 iframe 的显/隐
    const isChatHubPage = ref(false)
    // 这里是个优化,想的是只有页面访问过该路由才渲染,没访问过就不渲染该组件
    const chatHubVisited = ref(false) watch(() => routes.path,(value) => {if (value === '/chathub') {chatHubVisited.value = trueisChatHubPage.value = true} else {isChatHubPage.value = false}},{immediate: true}
    )
    
  4. ChatHub.vue组件代码(有单个或者多个iframe情况)
    <template><div class="iframe-container"><iframev-for="(item, index) in iframeList"v-show="showIframe(item, index)":key="item.url":src="item.url"frameborder="0"></iframe></div>
    </template>
    <script lang="ts" setup>
    export default {name: "ChatHub",
    };
    import { ref, reactive } from "vue";
    import { useRoute, useRouter } from "vue-router";
    const route = useRoute();const iframeList = reactive([{name: 1, url: "https://xxx"},{name: 2, url: "https://yyy"}
    ])// 是否显示
    const showIframe = (item, index) => {if (route.query.url === item.url) {return true;} else {return false;}
    };</script>

http://www.15wanjia.com/news/184568.html

相关文章:

  • 怎么建淘宝优惠券网站做推广页面跳转的方式有哪些
  • 某企业网站建设论文徐州市专业做网站的公司
  • 自己做网站好还是凡科知名设计公司有哪些
  • 连云港网站建设哪家好滨江网站建设公司
  • 洛阳市住房和城乡建设局网站h5网页设计软件
  • 网站开发实用技术网站建设分项报价表
  • 惠州网站制作费用网站关键字收录
  • app网站建设 - 百度苏州公司注册费用
  • 做网站系统的过程珠海网站设计哪家好
  • 企业公司建设网站企业微网站怎么做
  • 免费的网站源码去哪下载邯郸媒体网络营销诚信合作
  • 用php做企业网站的可行性做国外网站汇款用途是什么
  • 游戏网站建设免费门户网站策划书
  • 网站策划的内容有那些外贸网站产品关键词
  • 导航网站网站提交怎么做北京海淀区房价
  • 佛山网站建设哪家专业网站icp备案网址
  • 网站接入变更网站支付页面怎么做
  • 如何彻底清除网站的网页木马注册网站不用手机短信验证的
  • 电子商务网站建设的方法及流程图广东省第二中医院官网进入公众号
  • 做h5场景的网站购买网站空间送域名
  • 网站开发方倍工作室杭州模板建站软件
  • 设计网络网站有哪些功能积分商城系统
  • 柳州做网站那家好wordpress自定义文章代码和样式
  • 湖北专业网站建设产品介绍智慧团建官方网站
  • 微信手机网站搭建网站logo怎么做最清楚
  • 学做的网站基础蛋糕网站建设范围
  • 网站模板下载后怎么用搜索引擎官网
  • 网站管理文档怎么写山东省住房城乡建设厅门户网站
  • 嘉兴百度网站推广长治市建设局网站
  • 学院网站建设项目的成本计划书wordpress支持大数据处理