vue 循环动态设置ref并获取$refs方式

作者:zeng092210 时间:2023-07-02 17:00:18 

vue循环动态设置ref并获取$refs

关于ref的使用和场景请看官网文档

下面是我对循环设置ref并获取使用的一些办法,简单记录一下

一. 使用理解步骤(个人理解、大白话概况)

单循或双循环标签使用,无论那种都要**保证ref唯一**

例如:可以在ref值后面附带id或不会重复的索引

针对上述唯一的ref进行获取

获取:单循环可以直接用$refs获取非单循环可以通过eval()获取

二. 单循环和无循环动态设置ref

设置 【ref=“xxx”】【ref="‘xxx’+index"】

就不多说了 简单

三. 双循环动态设置ref

设置【:ref="‘xxx’+id"】或【:ref="‘xxx’+index"】

<div v-for="(item,index) in topicList" :key="index">
      <el-carousel-item v-for="(son,i) in item.questionList" :key="index+i">
           <topic :ref="'topicRef'+son.bId"></topic>
           //或也可以用索引.  用一个索引会重复,如下
           //<topic :ref="'topicRef'+(i+index)"></topic>
      </el-carousel-item>
</div>

获取

eval("that.$refs.tagItem" +(x+index))[0]

eval("that.$refs.topicRef" +(ele.bId))[0]

ref和$refs的使用方法

在JavaScript中需要通过document.querySelector("#demo")来获取dom节点,然后再获取这个节点的值。

在Vue中,我们不用获取dom节点,元素绑定ref之后,直接通过this.$refs即可调用,这样可以减少获取dom节点的消耗。

ref介绍

ref被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向该子组件实例

通俗的讲,ref特性就是为元素或子组件赋予一个ID引用,通过this.$refs.refName来访问元素或子组件的实例

<p ref="p">Hello</p>
<children ref="children"></children>
this.$refs.p
this.$refs.children

this.$refs介绍

this.$refs是一个对象,持有当前组件中注册过 ref特性的所有 DOM 元素和子组件实例

注意: $refs只有在组件渲染完成后才填充,在初始渲染的时候不能访问它们,并且它是非响应式的,因此不能用它在模板中做数据绑定

案例一、ref 写在标签上时

<!-- ref 写在标签上时:this.$refs.名字  获取的是标签对应的dom元素
    ref 写在组件上时:这时候获取到的是 子组件(比如counter)的引用-->
   <div id="root">
       <!-- ref = 'hello': 给div 起了一个引用的名字 hello -->
       <div
           ref = 'hello'
           @click = "handleClick">
           laugh yourself
       </div>
   </div>

<script>
       var vm = new Vue({
           el: '#root',
           methods: {
               handleClick: function() {
                   //this.$refs : 获取整个Vue实例中所有的引用 然后再找到hello这个引用 指向div那个dom节点
                   //console.log(this.$refs.hello)
                   alert(this.$refs.hello.innerHTML)
               }
           }
       })
   </script>

案例二、 ref 写在组件上时

计数:

<!-- ref 写在标签上时:this.$refs.名字  获取的是标签对应的dom元素
    ref 写在组件上时:这时候获取到的是 子组件(比如counter)的引用-->
   <div id="root">
       <!-- 子组件触发了事件 这里父组件(模板区里面)监听该事件 调用handleChange方法
                                   因此handleChange方法定义在父组件的methods里面-->
       <counter ref="one" @change="handleChange"></counter>

<counter ref="two" @change="handleChange"></counter>
       <div>{{total}}</div>
   </div>

<script>
       Vue.component('counter', {
           template: '<div @click="handleClick"> {{number}} </div>',
           data: function() {
               return {
                   number: 0
               }
           },
           methods: {
               handleClick: function() {
                   this.number ++
                   //子组件向父组件传值  子组件被点击的时候 number+1  同时告诉外面 也即是触发一个事件
                   this.$emit('change')
               }
           },
       })

var vm = new Vue({
           el: '#root',
           data: {
               total: 0
           },
           methods: {
               handleChange: function() {
                   //在此方法中计算两个数量的和  通过this.$refs.引用名字 获取两个子组件的引用
                   this.total = this.$refs.one.number +
                                   this.$refs.two.number
               }
           }
       })
   </script>

注意:

当ref和v-for一起使用时,获取到的引用将会是一个数组,包含循环数组源

<template>
 <div>
   <div ref="myDiv" v-for="(item, index) in arr" :key="index">{{item}}</div>
 </div>
</template>

<script>
export default {
 data() {
   return {
     arr: ['one', 'two', 'three', 'four']
   }
 },
 mounted() {
   console.log(this.$refs.myDiv)
 },
 methods: {}
}
</script>

<style lang="sass" scoped>

</style>

vue 循环动态设置ref并获取$refs方式

实例(通过ref特性调用子组件的方法)

【1】子组件code:

<template>
   <div>{{msg}}</div>
</template>

<script>
export default {
 data() {
   return {
     msg: '我是子组件'
   }
 },
 methods: {
   changeMsg() {
     this.msg = '变身'
   }
 }
}
</script>

<style lang="sass" scoped></style>

【2】父组件code:

<template>
 <div @click="parentMethod">
   <children ref="children"></children>
 </div>
</template>

<script>
import children from 'components/children.vue'
export default {
 components: {
   children
 },
 data() {
   return {}
 },
 methods: {
   parentMethod() {
     this.$refs.children  //返回一个对象
     this.$refs.children.changeMsg() // 调用children的changeMsg方法
   }
 }
}
</script>

<style lang="sass" scoped></style>

来源:https://blog.csdn.net/zeng092210/article/details/111612280

标签:vue,循环,ref,$refs
0
投稿

猜你喜欢

  • Python探索之Metaclass初步了解

    2023-12-22 10:25:37
  • mysql创建数据库,添加用户,用户授权实操方法

    2024-01-14 18:16:58
  • asp.net(c#)实现从sqlserver存取二进制图片的代码

    2023-06-26 21:48:03
  • MySQL临时表的简单用法介绍

    2024-01-13 10:03:56
  • MySQL中存储的数据查询的时候如何区分大小写

    2024-01-21 12:01:27
  • pandas通过索引进行排序的示例

    2021-04-21 04:19:55
  • PHP abstract 抽象类定义与用法示例

    2023-06-28 03:52:44
  • Python中类和对象的绑定及非绑定方法详解

    2023-03-13 12:27:32
  • 三达不溜:www

    2009-03-28 11:44:00
  • python函数超时自动退出的实操方法

    2021-09-11 07:07:45
  • python 安装virtualenv和virtualenvwrapper的方法

    2023-04-23 11:27:33
  • .Net Core SDK命令介绍及使用

    2024-05-13 09:16:00
  • 常用SQL语句优化技巧总结【经典】

    2024-01-20 19:27:03
  • JScript 的内存回收

    2007-10-16 20:02:00
  • keras.layers.Conv2D()函数参数用法及说明

    2022-02-25 01:15:05
  • 了解ASP的基本语法和变量

    2008-01-16 13:03:00
  • 对Python 中矩阵或者数组相减的法则详解

    2024-01-02 01:29:17
  • python matplotlib:plt.scatter() 大小和颜色参数详解

    2021-09-26 04:56:28
  • SQL批量插入数据几种方案的性能详细对比

    2024-01-13 11:06:32
  • Python for循环通过序列索引迭代过程解析

    2021-11-30 05:36:50
  • asp之家 网络编程 m.aspxhome.com