Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: 页面初始化时先加载pinia再加载router,兼容更多使用场景 #649

Merged
merged 1 commit into from
Jul 18, 2023

Conversation

hxzAugustinus
Copy link
Contributor

使用中发现,如果路由中有涉及使用到store里的变量, 且想做持久化时;会由于先加载路由后加载store ,导致持久化失败;
所以mian 里初始化 应该按顺序 加载plugin(如果有依赖关系)。

@xiaoxian521
Copy link
Member

xiaoxian521 commented Jul 18, 2023

嘿,先加载Vue Router是较为常见的做法。对于您的项目可能需要先加载pinia,自行改一下即可哈,但对于平台来说 还是优先加载Vue Router

@hxzAugustinus
Copy link
Contributor Author

image

@xiaoxian521
从这个截图里是可以看出,基本上 router 初始化及做一些拦截时都会读取store里的 一些变量和调用函数;
所以可以认为 Vue Router 需要依赖 pinia ;

另外一个就是我当时想添加一下 pinia 的持久化 ;加完会直接持久化无效 且没有任何报错,也无法debug ;
花了差不多1天多的时间才发现问题在这个顺序上 。

基于 插件的依赖 解释加载顺序 和 pinia的持久化很多人会使用(不然debug很困难),所以推荐改一下加载顺序。

@xiaoxian521
Copy link
Member

你改完顺序使用过程中 有什么bug吗 没有的话 抽空我测试下

@xiaoxian521 xiaoxian521 reopened this Jul 18, 2023
@hxzAugustinus
Copy link
Contributor Author

我改了一下之后本地跑了一遍没啥问题(没有覆盖测试到所有功能点) ;pinia 的持久化 ,按文档添加直接成功也没问题

@hxzAugustinus
Copy link
Contributor Author

hxzAugustinus commented Jul 18, 2023

@xiaoxian521 我写了一个简单的demo ,你可以看一下

https:/hxzAugustinus/vue-pure-admin/tree/test-pinia-persist

使用上面的链接 运行 ,然后触发登录,会发现无法持久化 pure-permission

修改main.ts的加载顺序
image

再重新运行,然后触发登录 , 可以在localstorage 里找到 pure-permission 对象存储

主要原因有2个:

  1. 05692e9#r121965409 这里路由读取使用了一下 pure-permission 里的变量
  2. main 里的 插件加载顺序

/----------------------------------------------------------------------------/

整个过程无任何报错,也没有任何警告提示 , 所以非常难以发现问题所在

@xiaoxian521 xiaoxian521 changed the title perf(main): 先于路由router 加载 store perf: 页面初始化时先加载pinia再加载router,兼容更多使用场景 Jul 18, 2023
@xiaoxian521 xiaoxian521 merged commit d27c062 into pure-admin:main Jul 18, 2023
2 checks passed
@xiaoxian521 xiaoxian521 added the test:pass 测试通过 label Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test:pass 测试通过
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants