<div>
<title-bar />
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" :key="$route.name" v-if="switchView(Component)" />
</keep-alive>
</router-view>
<!-- 把webvie的页面缓存起来放在router-view外层,这样不会导致webview刷新 -->
<component :is="keepAliveWebview" v-show="keepAliveWebview !== null && !noWebview" />
</div>
// 解决切换路由会导致webview刷新的问题
const noWebview = ref(true)
const saveKeepAlivePath = ref('/pcHome')
const keepAliveWebview = ref<any>(null)
// 这里获取router切换的component
const switchView = (component: any) => {
noWebview.value = true
// 通过path判断是否是webview的页面
if (route.path === saveKeepAlivePath.value) {
noWebview.value = false
// 把webview页面的component存起来
if (!keepAliveWebview.value) {
keepAliveWebview.value = component
}
}
return noWebview.value
}
如果有更好的方法欢迎大家在下方讨论