BT1*_*101 2 vue.js server-side-rendering vuex vuejs2 nuxt.js
我正在开发 vue2+nuxt 应用程序,我正在使用 vuex+persisted state 包。在其中一个组件中,我根据状态布尔变量显示带有条件的 div:
<template>
<div class="nav__topbar" v-if="show">
....
</div>
</template>
<script>
export default {
computed: {
show() {
return this.$store.state.navInfo.navInfo
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
一切正常 - 如果this.$store.state.navInfo.navInfo为真,则显示,否则显示消失。同样在刷新后它仍然有效。
我无法解决的唯一问题是,在刷新 div 显示一秒钟后布尔值为 false 时。它在说 0.2 秒后消失了,但即使它如此之快,它仍然会使页面“跳跃”,因为它.nav__topbar是 100% 宽度和 20vh 高度。因此,有一段时间我可以看到这个 div,然后它会隐藏并且所有页面都跳起来,这看起来非常难看,我无法忽略它。
有什么办法可以防止这种行为吗?
也许我可以使用 Nuxt 提供的这个 fetch() 或 asyncData 钩子?
在我发现这条评论之前,我在最后几天遇到了同样的问题
插件/persistedstate.js
import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
import cookie from 'cookie'
export default ({store, req, isDev}) => {
createPersistedState({
key: 'your_key',
paths: ['state1', 'state2',...so_on],
storage: {
getItem: (key) => process.client ? Cookies.getJSON(key) : cookie.parse(req.headers.cookie||'')[key],
setItem: (key, value) => Cookies.set(key, value, { expires: 365, secure: !isDev }),
removeItem: (key) => Cookies.remove(key)
}
})(store)
}
Run Code Online (Sandbox Code Playgroud)
nuxt.config.js
plugins: [
{ src: '~plugins/persistedstate.js' }
]
Run Code Online (Sandbox Code Playgroud)
解决了我的问题,希望它也能解决你的问题。我唯一认识到的是该函数现在在服务器和客户端上都被调用。将尝试找出原因。