Pass props based on tests within computed properties
P粉875565683
P粉875565683 2023-08-17 19:18:01
0
1
515
<p>I ran into a problem with a simple test in Nuxt 3's <code>computed()</code> property. </p> <pre class="brush:php;toolbar:false;">const test = computed(() => { if (process.client) { console.log('Worked. Is it a mobile device?', window.innerWidth < 768) return window.innerWidth < 768 } else { console.log('Didn't work') return } })</pre> <p>The result of the computed property is always correct, but I want to use it in the template below to pass props conditionally. </p> <pre class="brush:php;toolbar:false;"><Loader v-if="isLoading" :images="test ? brands.desktopLoaderImages : brands.mobileLoaderImages" /></pre> <p>The problem is that regardless of the result, brands.mobileLoaderImages is always passed as props to my component and I can't figure out why. </p> <p>I tried using a different technique to determine the screen size other than <code>window.innerWidth</code>, like a dedicated module like <code>@vueuse/core</code> but the result it's the same. I'm guessing the problem might be from Vue's lifecycle or something? </p>
P粉875565683
P粉875565683

reply all(1)
P粉682987577

It is recommended to use useBreakpoints Use its nuxt module from VueUse :

const breakpoints = useBreakpoints()

const test  = breakpoints.smaller('md')
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template