heroMan
heroMan
  • 发布:2025-04-03 18:59
  • 更新:2025-04-14 11:29
  • 阅读:169

this.$refs undefined

分类:nvue

被uniapp搞的没脾气了
为什么this.$refs会是undefined呢???
我看官方的uni-indexed-list组件里面就在用的啊 而且console.log了下是正常的。
我的代码很简单:

<list scrollable="true" show-scrollbar="false" style="flex: 1; background-color: aliceblue;" @click="bindClick">  
            <cell v-for="(list, idx) in state.pickerOptions" :key="idx" :ref="'' + idx" @click="bindClick">  

            </cell>  
        </list>  

 function bindClick() {  
      console.log('bindClick: ', this.$refs) // undefined  
  }

环境是vue3,组合式,nvue页面

2025-04-03 18:59 负责人:无 分享
已邀请:
DCloud_UNI_JBB

DCloud_UNI_JBB

试试这样

<template>  
    <view class="main-wrapper">  
        <view class="header">  
            <view class="search">  
                <input placeholder="搜索..." />  
            </view>  
        </view>  
        <view class="content" ref="nodeRef">  
            <view v-for="(item, idx) in 3" :ref="el => uploadRefs[idx] = el">sample {{ item }}</view>  
        </view>  
        <button @touchend="handleTouchEnd">click me</button>  
    </view>  
</template>  

<script setup>  
    import {  
        onMounted,  
        ref,  
        getCurrentInstance  
    } from 'vue'  

    const nodeRef = ref(null)  
    const uploadRefs = ref([])  

    onMounted(() => {  
        console.log(nodeRef.value, 'single ref')  
        console.log(uploadRefs.value, 'multiple refs')  
    })  
</script>
DCloud_UNI_JBB

DCloud_UNI_JBB

我明天试下

DCloud_UNI_JBB

DCloud_UNI_JBB

组合式这样写试试

<template>  
    <view class="main-wrapper">  
        <view class="header">  
            <view class="search">  
                <input placeholder="搜索..." />  
            </view>  
        </view>  
        <view class="content" ref="nodeRef">  
            <view>sample</view>  
            <view>sample</view>  
            <view>sample</view>  
            <view>sample</view>  
            <view>sample</view>  
            <view>sample</view>  
            <view>sample9</view>  
        </view>  
    </view>  
</template>  

<script setup>  
    import {  
        onMounted,ref  
    } from 'vue'  

    const nodeRef = ref(null)  

    onMounted(() => {  
        console.log(nodeRef.value, 'ref')  
    })  
</script>
  • heroMan (作者)

    需要动态的~ 所以才:ref= 这样写了

    2025-04-14 11:11

要回复问题请先登录注册