vue3 路由跳转空白,刷新页面不会空白

944 阅读1分钟

vue3 路由跳转空白,刷新页面不会空白

原因是,没有给router-view动态添加key,key根据路径+随机数即可

<template>
    <div class="app-main-box">
      <router-view :key="key"/>
    </div>
</template>

<script setup lang="ts">
import { computed } from '@vue/runtime-core';
import { useRoute } from 'vue-router';

const route = useRoute();
const key = computed(() => {
  return route.path + Math.random();
});
</script>