Vue2
<button @click="handleRefresh">刷新</button>
<keep-alive v-if="isRefresh">
<router-view />
</keep-alive>
<script>
export default {
data() {
return {
isRefresh:true
};
},
methods: {
handleRefresh(){
this.isRefresh = false
this.$nextTick(()=>{
this.isRefresh = true
})
}
}
};
</script>
Vue3
<template>
<button @click="handleRefresh">刷新</button>
<router-view v-slot="{ Component }">
<keep-alive v-if="isRefresh">
<component :is="Component" :key="$route.name" />
</keep-alive>
</router-view>
</template>
<script lang=ts>
import { defineComponent, ref , nextTick} from 'vue'
export default defineComponent({
name: 'app',
props: {},
components: {},
setup: () => {
const isRefresh = ref(true)
const handleRefresh =()=>{
isRefresh.value = false
nextTick(()=>{
isRefresh.value = true
})
}
return {handleRefresh,isRefresh}
}
})
</script>