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