Vue router keep iframe dom element (just hide, don't destroy)

[亡魂溺海] 提交于 2019-12-22 14:09:17

问题


I'm developing a Vue 2.5 SPA using vue-router 3.0.

Some views contain subcomponents that render iframes.

When I switch to another route and return to a route that renders an iframe, the respective iframe is reloaded, even if it was visited before. This behavior is unwanted because it results in a bad UX in that case. The iframe state shall remain the same as before, when the user returns to the view.

I guess the reloading is caused by the dom being destroyed when leaving the route. Even the following structure doesn't prevent it:

<keep-alive>
  <router-view></router-view>
</keep-alive>

<keep-alive> seems to keep the Vue component itself alive, but not the dom representation.

Is there any way (or workaround) to keep the dom when switching routes? A router-mode that would allow for hiding instead of swapping the components would be perfect.


回答1:


You will need to use a v-show on your iframe. On route, you will not specify the component

<template>
    <iframe v-show="$route.path == '/foo'">
</template>

<script>
routes: [
        { path: '/foo'},
        { path: '/bar' }
    ]
</script>

p.s... i'm looking for a more elegant way to do that... should be a property on router to tell that we want to hide, not destructed/recreated



来源:https://stackoverflow.com/questions/47996614/vue-router-keep-iframe-dom-element-just-hide-dont-destroy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!