search

Home  >  Q&A  >  body text

Pass props based on tests within computed properties

<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粉875565683P粉875565683467 days ago463

reply all(1)I'll reply

  • P粉682987577

    P粉6829875772023-08-18 18:12:41

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

    const breakpoints = useBreakpoints()
    
    const test  = breakpoints.smaller('md')
    

    reply
    0
  • Cancelreply