首页  >  问答  >  正文

根据计算属性内的测试传递props

<p>我在Nuxt 3的<code>computed()</code>属性中遇到了一个简单测试的问题。</p> <pre class="brush:php;toolbar:false;">const test = computed(() => { if (process.client) { console.log('工作了。是移动设备吗?', window.innerWidth < 768) return window.innerWidth < 768 } else { console.log('没有工作') return } })</pre> <p>计算属性的结果总是正确的,但是我想在下面的模板中使用它来有条件地传递props。</p> <pre class="brush:php;toolbar:false;"><Loader v-if="isLoading" :images="test ? brands.desktopLoaderImages : brands.mobileLoaderImages" /></pre> <p>问题是无论结果如何,brands.mobileLoaderImages总是作为props传递给我的组件,我无法弄清楚为什么。</p> <p>我尝试使用不同的技术来确定屏幕大小,而不是<code>window.innerWidth</code>,比如像<code>@vueuse/core</code>这样的专用模块,但结果都一样。我猜问题可能来自Vue的生命周期或其他什么东西?</p>
P粉875565683P粉875565683428 天前424

全部回复(1)我来回复

  • P粉682987577

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

    建议使用useBreakpoints从VueUse中使用它的nuxt模块

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

    回复
    0
  • 取消回复