entry-client.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { createApp } from './main'
  2. const { app, router, store } = createApp()
  3. if(window.__INITIAL_STATE__){
  4. store.replaceState(window.__INITIAL_STATE__)
  5. }
  6. // router.beforeEach((to,from,next)=>{
  7. // const uskyDb = new indexedDb("uskyDb") //创建或连接DB数据库
  8. // uskyDb.openStore("menu","id",['list']).then((res:any)=>{
  9. // next()
  10. // })
  11. // })
  12. router.isReady().then(() => {
  13. //比较to、from不同时执行数据预取
  14. router.beforeResolve((to,from,next)=>{
  15. let toComponents = router.resolve(to).matched.flatMap(record=>Object.values(record.components))
  16. let fromComponents = router.resolve(from).matched.flatMap(record=>Object.values(record.components))
  17. let actived = toComponents.filter((c,i)=>{
  18. return fromComponents[i] !==c
  19. })
  20. if(!actived.length){
  21. return next()
  22. }
  23. //对所有匹配的路由组件调用`asyncData()
  24. Promise.all(actived.map((Component:any) =>{
  25. if(Component.asyncData){
  26. return Component.asyncData({
  27. store,
  28. route: router.currentRoute
  29. })
  30. }
  31. })).then(()=>{
  32. next()
  33. })
  34. })
  35. //页面刷新
  36. const matchedComponents = router.currentRoute.value.matched.flatMap(record =>Object.values(record.components))
  37. //对所有匹配的路由组件调用`asyncData()
  38. Promise.all(matchedComponents.map((Component:any) =>{
  39. if(Component.asyncData){
  40. return Component.asyncData({
  41. store,
  42. route: router.currentRoute
  43. })
  44. }
  45. }))
  46. app.mount('#app')
  47. })