@@ -8,7 +8,7 @@ import { useConfetti } from '~/composables/useConfetti.js'
88const { playConfettiAnimation } = useConfetti ( )
99
1010// 全局前置守卫
11- let hasGetInfo = false // 处理点击一次菜单就会获取两次 getInfo 的问题
11+ let hasGetInfo = false // 处理点击一次菜单就会获取两次 getInfo 的问题
1212router . beforeEach ( async ( to , from , next ) => {
1313 // 显示全屏 loading
1414 showFullLoading ( )
@@ -27,7 +27,8 @@ router.beforeEach(async (to, from, next) => {
2727 let hasNewRoutes = false
2828 // 如果用户登录了,自动获取用户信息并存储在 vuex 中
2929 // 并且防止重复获取用户信息 getInfo,加快点击菜单的反应速度
30- if ( token && ! hasGetInfo ) { // 同为真才会走下面的逻辑,这里 !hasGetInfo 也可以换成 hasGetInfo === false
30+ if ( token && ! hasGetInfo ) {
31+ // 同为真才会走下面的逻辑,这里 !hasGetInfo 也可以换成 hasGetInfo === false
3132 let { menus } = await store . dispatch ( 'getInfo' )
3233 hasGetInfo = true
3334 // 动态添加路由
@@ -41,9 +42,20 @@ router.afterEach((to, from) => {
4142 document . title = to . meta . title + '-商城后台管理系统'
4243 // 关闭全屏 loading
4344 hideFullLoading ( )
44-
45- // 只在页面刷新时触发烟花动画(页面刷新时from.name为null)
46- if ( ! from . name ) {
47- playConfettiAnimation ( )
48- }
49- } )
45+
46+ // 只在页面刷新时触发烟花动画(页面刷新时from.name为undefined)
47+ // 页面刷新时from.name为undefined,触发烟花动画
48+ if ( ! from . name ) playConfettiAnimation ( )
49+
50+ /**
51+ * 第二种方法:<短路求值>
52+ * 1. from 是路由守卫提供的参数,表示用户从哪个路由来
53+ * 2. from.name 是来源路由的名称
54+ * 3. !from.name 判断是否没有来源路由名称
55+ * 页面刷新时:from.name 为 undefined,所以 !from.name 为 true,执行 playConfettiAnimation()
56+ * 正常导航时:from.name 有值,所以 !from.name 为 false,不执行 playConfettiAnimation()
57+ * 4. 短路求值:只有当 !from.name 为 true 时才会执行 playConfettiAnimation()
58+ * 5. 这样设计可以让烟花动画只在用户刷新页面时显示,而不是每次页面导航都显示,提供更好的用户体验。
59+ * !from.name && playConfettiAnimation()
60+ */
61+ } )
0 commit comments