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.pthis.$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

相关文章:

  • 2021-12-03
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2021-06-07
  • 2021-05-17
猜你喜欢
  • 2023-03-10
  • 2022-12-23
  • 2021-08-14
  • 2021-05-20
  • 2023-01-23
  • 2023-01-23
相关资源
相似解决方案