un-pany / v3-admin-vite

☀️ A vue3 admin template | vue3 admin/element plus admin/vite admin/vue3 template/vue3 后台/vue3 模板/vue3 后台管理系统
https://un-pany.github.io/v3-admin-vite
MIT License
5.26k stars 890 forks source link

首页第一次加载时不触发 onActivated 钩子 #190

Closed goooogs closed 4 days ago

goooogs commented 5 months ago

问题描述

在首页开启 keepAlive 的情况下,第一次访问首页时不能正常触发 onActivated 钩子

环境

代码版本:v4.4.0-30-g7837bac npm: 10.2.3 node: v20.10.0

复现步骤

  1. @/router/index.ts 中为 Dashboard 添加 keepAlive: true
  2. @/views/dashboard/index.vuescript setup 中添加如下调试代码
    
    import { onMounted, onActivated, onDeactivated, onUnmounted } from 'vue'

defineOptions({ name: 'Dashboard' })

onActivated(() => console.log('[dashboard] onActivated')) onDeactivated(() => console.log('[dashboard] onDeactivated')) onMounted(() => console.log('[dashboard] onMounted')) onUnmounted(() => console.log('[dashboard] onUnmounted'))


3. `pnpm dev` 启动服务,登录后直接进入首页,控制台只输出 `[dashboard] onMounted`。切换到其它页面能够正常触发 `onDeactivated` 钩子,再回到首页时也能正常触发 `onActivated` 钩子,如下图
<img width="434" alt="image" src="https://github.com/un-pany/v3-admin-vite/assets/8554557/9471e486-baa6-4a96-b543-b58aaa5fc4ac">

### 补充说明
如果把 `@/layouts/components/AppMain.vue` 中的 `include` 去掉,即:把 `<keep-alive :include="tagsViewStore.cachedViews">` 修改为 `<keep-alive>` 也能正常触发 `onActivated` 钩子

### 期望
首页开启 `keepAlive` 后,第一次访问首页时能正常触发 `onActivated` 钩子
QC2168 commented 1 month ago

试了下,没有在router设置keepAlive也是只有打印onMounted

pany-ang commented 4 days ago

已修复