简体   繁体   English

Nuxt.JS根本不需要渲染块

[英]Nuxt.JS doesen't render blocks as I need

I'm building nuxt.js SSR web app and I face strange issue. 我正在构建nuxt.js SSR网络应用程序,我面临着奇怪的问题。

<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. 我将通过几个步骤解释这一点。

  1. Let me explain how it should work: 让我解释它应该如何工作:
    • Div with class="class1" should be displayed by default on Desktop and dissapear when you hover container. 默认情况下,桌面上应显示带有class =“class1”的Div,并在悬停容器时显示消失。 On mobile it should be hidden everytime. 在移动设备上,它应该每次都隐藏起来。
    • Div with class="class2" should be hidden on Desktop and appear when you hover container. Div = class =“class2”的Div应隐藏在桌面上,并在您悬停容器时显示。 On mobile it should be displayed everytime. 在移动设备上,它应该每次都显示出来。
  2. Let me explain how it works now: 让我解释它现在是如何工作的:
    1. Desktop: 桌面:
      • Div with class="class1" not displayed untill I hover container once, then works as it should 没有显示 class =“class1”的div, 直到我将容器悬停一次,然后按预期工作
      • Div with class="class2" works as it should. 带class =“class2”的div可以正常工作。
    2. Mobile: 移动:
      • Div with class="class1" works as it should. 带class =“class1”的div可以正常工作。
      • Div with class="class2" works as it should. 带class =“class2”的div可以正常工作。
  3. How did I fix it: 我是如何解决它的:
    • After few hours of trying I just realised that I can rewrite v-show on first div to v-show="!hovered" and set media query on mobile screen display:none; 经过几个小时的尝试,我才意识到我可以将第一个div上的v-show="!hovered"重写为v-show="!hovered"并在移动屏幕上设置媒体查询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" ? 但是当我有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" . 我猜测Nuxt.JS上的第一个加载在服务器上,所以isDesktop返回为undefined,因此v-show="!hovered && isDesktop"变成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 . 但是为什么第二个div的v-show指令: v-show="hovered || !isDesktop"可以正常工作,如果它应该变成v-show="false || !undefined"但我仍然将这个div 隐藏在桌面上显示在手机上

PS This is my first StackOverflow question, sorry if I styled code badly, I dont get how it works. PS这是我的第一个StackOverflow问题,对不起,如果我的代码很糟糕,我不知道它是如何工作的。 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. 当我尝试时,Nuxt甚至不会编译它。 I wonder why your Nuxt server can work with this. 我想知道为什么你的Nuxt服务器可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM