vue ref作用详解

64 篇文章 1 订阅
17 篇文章 0 订阅

ref 有三种用法:

  1、ref 加在普通的元素上,用this.ref.name 获取到的是dom元素

<div id="app">
    <div ref="testDom">11111</div>
    <button @click="getTest">获取test节点</button>
  </div>


this.$refs.p

  2、ref 加在子组件上,用this.ref.name 获取到的是组件实例,可以使用组件的所有方法

  3、如何利用 v-for 和 ref 获取一组数组或者dom 节点

ref 被用来给元素或子组件应用信息,引用信息将会注册到父组件的$ref对象上,

如果在普通的dom元素上,引用指向的就是dom元素;

如果在子组件上,引用就是指向组件实例

this.$refs介绍

$refs 只是在组件宣染完才填充,而不是响应式的。不能再模板中做数据绑定。

他仅仅是一个直接操作子组件的应急方案,要避免在模板或计算属性使用$refs

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

     1、ref 需要在dom渲染完成后才会有,在使用的时候确保dom已经渲染完成。

            比如在生命周期 mounted(){} 钩子中调用

            或者在 this.$nextTick(()=>{}) 中调用

  2、如果ref 是循环出来的,有多个重名,那么ref的值会是一个数组 ,此时要拿到单个的ref 只需要循环就可以了

子组件

<template>
   <p>{{str}}</p>
</template>

<script>
    export default{
        data(){
            str:'子组件' 
        }
   },
   
   methods:{
     add(){
        this.str = '你好'
     }
   }

</script>

父组件

<p @click = fuadd()>
   <children ref= 'children '></children>
</p>

<script>
    import  children from'../children.vue'
    export default{
        components:{
            children
        },
        data(){
            return{
                msg2:''
            }

        },
       methods:{
          fuadd(){
             this.$refs.children 返回一个对象
             this.$refs.children.add() 调用子组件的方法
             this.$refs.children.str 读取子组件的值
             this.$refs.children.textContent  读取子组件的内容

             this.$nextTick(() => {
                this.msg2 = this.$refs.children.innerHTML
            })

            this.msg3 = this.$refs.children.innerHTML

          }
       }
    }
</script>

ref 使用在外面的组件上

<div id="ref-outside-component" v-on:click="consoleRef">
    <component-father ref="outsideComponentRef">
    </component-father>
    <p>ref在外面的组件上</p>
</div>
//组件/
  var refoutsidecomponentTem={
       template:"<div class='childComp'><h5>我是子组件</h5></div>"
   };
    var refoutsidecomponent=new Vue({
        el:"#ref-outside-component",
        components:{
            "component-father":refoutsidecomponentTem
        },
        methods:{
            consoleRef:function () {
                console.log(this); // #ref-outside-component     vue实例
                console.log(this.$refs.outsideComponentRef);  // div.childComp vue实例,组件实例
            }
        }
    });

ref 用到元素上


<div id="ref-outside-dom" v-on:click="consoleRef" >
   <component-father>
   </component-father>
   <p ref="outsideDomRef">ref在外面的元素上</p>
</div>

    var refoutsidedomTem={
        template:"<div class='childComp'><h5>我是子组件</h5></div>"
    };
    var refoutsidedom=new Vue({
        el:"#ref-outside-dom",
        components:{
            "component-father":refoutsidedomTem
        },
        methods:{
            consoleRef:function () {
                console.log(this); // #ref-outside-dom    vue实例
                console.log(this.$refs.outsideDomRef);  //  <p>标签dom元素 ref在外面的元素上</p>
            }
        }
    });

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小四是个处女座

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值