Solve el-menu incompatible SSR error
First it will report an error:Hydration completed but contains mismatches
, and issue aboutHydration
Warning!
Here is how I solved it step by step:
1. At the beginning, I really didn’t know how to solve it. There was no result in searching for error messages online. I only knew that it was el-menu incompatible ssr
2. Then enter the el-plus official website and finally found the clue:
Menu Menu
A menu that provides navigation functions for the website.
In the SSR scenario, you need to wrap the components in
<client-only></client-only>
Among them (such as: Nuxt) and SSG (: VitePress).
3. So I just addedclient-only
Tag, vue3 cannot recognize the tag, and it is discovered that this is based onNuxt
Framework tags
4. How to use the Vue3 projectclient-only
Woolen cloth? Baidu discovered such a plug-in latervue-client-only
Here is its npm link: /package/vue-client-only
5. So I immediately used it, but it was reported an error again... Then I went to the github official website of this plug-in: /egoist/vue-client-only and found that this was a project 4 years ago, so this must be based on vue2 incompatible with vue3! !
6. Then just when I didn’t know what to do, I clicked on this projectIssues
, I found that someone had asked a question
Is there a need to update component for usage with Vue 3? #122
The following answer finally saved me!
I created a small package for those who want to use the client-only component with Vue 3 outside a Nuxt project.
Check it here /duannx/vue-client-only
7. Enter the Vue3-encapsulatedvue-client-only
:/duannx/vue-client-only
Then follow the usage tutorial to resolve the error! ! ! !
npm install --save @duannx/vue-client-only
import ClientOnly from '@duannx/vue-client-only' <client-only> <el-menu :default-active="activeIndex" mode="horizontal" @select="handleSelect" > <el-menu-item index="orders">{{ t('') }}</el-menu-item> <el-menu-item index="records">{{ t('') }}</el-menu-item> <el-sub-menu index="language"> <template #title>{{ t('') }}</template> <el-menu-item index="zh">Simplified Chinese</el-menu-item> <el-menu-item index="en">English</el-menu-item> </el-sub-menu> <el-menu-item index="logout" v-if="userStatus"> {{ t("") }} </el-menu-item> <el-menu-item index="login" v-if="!userStatus"> {{ t("") }}/{{ t("") }} </el-menu-item> </el-menu> </client-only>
The above is the detailed content of solving the el-menu problem in the vue3 project. For more information about the el-menu incompatible SSR problem, please pay attention to my other related articles!