I am working on a NuxtJS project. For getting the current screen width in real-time, I created an instance variable based on Accessing screen width in vue components and “window is not defined” in Nuxt.js.
But, the value is not getting updated in real-time. Either there is a delay or I need to refresh. I just need only the current screen width if it is less than 500px it should be true or false.
plugins/breakpoints.js
import Vue from 'vue'
Vue.prototype.$isMobile = Vue.observable(window.innerWidth < 600)
window.addEventListener(
'resize',
() => (Vue.prototype.$isMobile = window.innerWidth < 600)
)
nuxt.config.js
plugins: [{ src: './plugins/breakpoints', ssr: false }],
pages/index.vue
<template>
<section>
<h1>isMobile {{ $isMobile }}</h1>
</section>
</template>
<script>
export default {
name: 'IndexPage',
}
</script>
CodeSandBox(for quick preview): https://codesandbox.io/p/sandbox/reverent-stitch-1sdh6r