how to use window.innerWidth inside computed Property - Nuxtjs

Asked

Viewed 33 times

0

am working on a Vue.js/nuxt.js project, where I need to determine styles within the computed Property and apply on a div based on screen size, as in the example below:

<template>
  <div :style="css"></div>
</template>

<script>
export default {
  computed: {
    css () {
      let width = window.innerWidth

      // ... mobile { ... styles }
      // ... desktop { ... styles }

      // ... if width is less than 700, return mobile
      // ... if width greater than 700, return desktop
    }
  }
}
</script>

It doesn’t work, it makes a mistake windows is note defined

in Nuxt.js, the window will only be available within the mounted, which is life cycle hook where the screen has already been rendered, which makes total sense.

I could put all this style logic inside a method and call on the Mounted, or put directly on the Mounted and apply on the div, the problem is that the style will be applied with a certain delay after the page is mounted.

Then how can I know the size of the page’s internal width inside the computed Property?

  • A file .vue has 3 sections, <template>, <script> and <style>. It wouldn’t be better if you added a CSS with media queries inside the tag <style>? I don’t understand why you’re styling in JS.

  • @fernandosavio, in this case no, because it is a reusable component, so the stylization will be dynamic and will have a certain level of complexity to the point that only assign a CSS class would not solve the problem, need to be done with script.

  • I get it, I believe there must be a better way to do this but this link demonstrates a way to do this..

  • Evan You himself advises you to do this inside the Lifecycle. so I don’t know if there’s a better way to do it

  • Take a look at this Codepen that I did, I believe it helps you. It would be a good idea if you made a mockery or a Throttle in the resize event not about loading the client.

  • @fernandosavio, I am already very grateful to be helping me, but the code you made, will not work in Nuxt.js largura: window.innerWidth, would generate windows is not defined as I said in my question, the window will only be available from mounted

  • The initial value can be either.. you can assign this value in the same Mounted. I just put it right for practicality, you need to adapt the code to your case

  • @fernandosavio, I can’t mess with mounted, as I said in my question, will apply the style with certain delay.. I’ve tried it, you can test it too.. places some div with full width and height, and in the mounted you apply a background to this div, when you load the page in the browser, or keep giving F5, you will notice that the page loads full, to then apply the background color, this is horrible.

  • @fernandosavio, I’m doing a Grid Layout component, so imagine, the page load all broken, only to adjust? cannot, need to have the style defined before reaching the mounted

  • It’s the price for using JS to style. But OK, I’ll follow the question to see if someone who understands has a way out. Good luck

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.