英文:
Vuetify components loading in mobile view first then after few seconds in desktop view in Nuxt 3
问题
我在Nuxt中有一个项目,我使用了vuetify 2组件,所以一开始当页面加载时,它会显示移动视图,也就是如果有人从移动设备查看页面时的样子。
几秒钟后,它会纠正为桌面视图,显示在桌面上。有人能告诉我为什么会发生这种情况吗?是vuetify组件的问题吗?
英文:
I have a project a in Nuxt and I have used vuetify 2 components so at first when the page loads it displays the mobile view meaning how the page would look if someone would view it from mobile.
After few seconds it rectifies that it is desktop so it displays the in desktop view .Can someone tell me why is this happening?Is it vuetify components problem?
答案1
得分: 0
这是一个通用问题,因为需要一些时间来完成hydration。有多种解决方法。
1- 在nuxt的最新版本中,您可以定义一个模板,直到hydration完成才进行渲染,这样就不会发生这种情况。
https://nuxt.com/blog/v3-6#spa-loading-indicator
2- 您可以定义vuetify的起始屏幕宽度,以便在hydration之前正确渲染。这需要您在JavaScript运行之前知道(猜测)屏幕宽度,通常是通过检查用户代理来实现。
以下是我在Nuxt 3中使用的示例代码。
import { createVuetify } from 'vuetify';
export default defineNuxtPlugin(nuxtApp => {
let ssr = true;
if (!nuxtApp.ssrContext?.event?.node?.req?.headers?.['user-agent']?.toLowerCase().includes('mobi')) {
ssr = {
clientWidth: 1281, // 初始渲染的所需宽度。这是一个'lg'断点宽度
};
}
const vuetify = createVuetify({
ssr,
});
nuxtApp.vueApp.use(vuetify);
});
英文:
This is a general problem because hydration takes a little time to happen. There are multiple fixes to this.
1- In the latest versions of nuxt, you can define a template to be rendered until hydration is finished so this wouldn't happen.
https://nuxt.com/blog/v3-6#spa-loading-indicator
2- You can define the starting screen width for the vuetify so it would know how to correctly render before hydration. This would require you to know (guess) the screen width before javascript runs, usually by inspecting the user agent.
This is an example code i use in Nuxt 3.
import { createVuetify } from 'vuetify';
export default defineNuxtPlugin(nuxtApp => {
let ssr = true;
if (!nuxtApp.ssrContext?.event?.node?.req?.headers?.['user-agent']?.toLowerCase().includes('mobi')) {
ssr = {
clientWidth: 1281, // the desired width for initial render. This is a 'lg' breakpoint width
};
}
const vuetify = createVuetify({
ssr,
});
nuxtApp.vueApp.use(vuetify);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论