I'm building nuxt.js SSR web app and I face strange issue.
<div class="container" @mouseenter="hovered = true" @mouseleave="hovered = false">
<transition name="fade">
<div class="class1" v-show="!hovered && isDesktop">blank_1</div>
</transition>
<transition name='scale'>
<div class="class2" v-show="hovered || !isDesktop">blank_2</div>
</transition>
</div>
<script>
export default {
data() {
return {
hovered: false
}
},
computed: {
isDesktop() {
if(process.client) {
window.screen.width > 1024 ? return true : return false
}
}
}
}
</script>
<style>
.class1 {
height: 100px;
width: 100px;
background-color: red;
}
.class2 {
height: 100px;
width: 100px;
background-color: blue;
}
</style>
I'll explain this by few steps.
v-show="!hovered"
and set media query on mobile screen display:none;
. So I fixed the only Desktop issue that I have. But why does it works this way when I have v-show="!hovered && isDesktop"
? I guessed that first load on Nuxt.JS goes on server, so isDesktop is returned as undefined so v-show="!hovered && isDesktop"
turns into v-show="!false && undefined"
. But then why second div's v-show directive: v-show="hovered || !isDesktop"
works fine if it should turn into v-show="false || !undefined"
but I still got this div hidden on Desktop and displayed on Mobile .
PS This is my first StackOverflow question, sorry if I styled code badly, I dont get how it works. Thank you for answers.
The weird behavior probably comes from this line
window.screen.width > 1024 ? return true : return false
when it should be
return window.screen.width > 1024 ? true : false
The syntax is incorrect. Nuxt won't even compile it when i tried. I wonder why your Nuxt server can work with this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.