hydration-strat-media.html 889 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <script src="../../dist/vue.global.js"></script>
  2. <div>resize the window width to < 500px to hydrate</div>
  3. <div id="app"><button>0</button></div>
  4. <script>
  5. window.isHydrated = false
  6. const {
  7. createSSRApp,
  8. defineAsyncComponent,
  9. h,
  10. ref,
  11. onMounted,
  12. hydrateOnMediaQuery,
  13. } = Vue
  14. const Comp = {
  15. setup() {
  16. const count = ref(0)
  17. onMounted(() => {
  18. console.log('hydrated')
  19. window.isHydrated = true
  20. })
  21. return () => {
  22. return h('button', { onClick: () => count.value++ }, count.value)
  23. }
  24. },
  25. }
  26. const AsyncComp = defineAsyncComponent({
  27. loader: () => Promise.resolve(Comp),
  28. hydrate: hydrateOnMediaQuery('(max-width:500px)'),
  29. })
  30. createSSRApp({
  31. setup() {
  32. onMounted(() => {
  33. window.isRootMounted = true
  34. })
  35. return () => h(AsyncComp)
  36. },
  37. }).mount('#app')
  38. </script>