app.vue
js
<template>
<div id="app">
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" v-if="$route.meta.keepAlive" />
</keep-alive>
<component :is="Component" v-if="!$route.meta.keepAlive" />
</router-view>
</div>
</template>
<script setup>
</script>
router.js
js
{
path: '/MPcard/MPcard',
name: 'MPcard',
component: () => import('../views/MPcard/MPcard.vue'),
meta: { title: "名片查询",keepAlive:true},
},
{
path: '/MPcard/MPecharts',
name: 'MPecharts',
component: () => import('../views/MPcard/MPecharts.vue'),
meta: { title: "名片图表" },
},
{
path: '/MPechartsZX/MPechartsZX',
name: 'MPechartsZX',
component: () => import('../views/MPcard/MPechartsZX.vue'),
meta: { title: "名片折线图表" },
},
从 名片查询页面进入到名片图表或着名片折线图表页面,返回的名片查询页面是不需要刷新的,但是从其它页面进来是需要刷新的
页面
js
import { ref, onMounted, nextTick, onActivated, onDeactivated } from "vue";
import { useRouter, useRoute, onBeforeRouteLeave } from "vue-router";
const chushih=()=>{
//这里就发送请求
}
onMounted(() => {
console.log('刷新了')
chushih();
});
const refreshFlag=ref('需要刷新')
onBeforeRouteLeave((to, from, next)=>{
const element = document.getElementById('mainContent').scrollTop
console.log('前往-to',to)
console.log('当前也-from',from)
console.log('离开了的时候当前页面的ID是mainContent的滚动高度',element)
data.value.scrollTop=element
// 这个是 进入数组里面的页面,需要缓存的
let array=['/MPcard/MPecharts','/MPechartsZX/MPechartsZX'] //从这几个页面返回的不需要刷新
if(array.includes(to.path)){
refreshFlag.value='不需要刷新'
}else{
refreshFlag.value='需要刷新'
}
next()
})
onActivated(()=>{
// console.log('回来了',refreshFlag.value)
if(refreshFlag.value=='需要刷新'){
chushih()
}else{
document.getElementById('mainContent').scrollTop=data.value.scrollTop
}
})