this.$refs使用方法

this.$refs

vueref可以以属性的形式添加给标签或者组件

ref 写在标签上时:this.$refs.ipt 获取的是添加了ref="ipt"标签对应的dom元素

ref 写在组件上时:this.$refs['component'] 获取到的是添加了ref="component"属性的这个组件

<template>
 //给标签使用
    <input type="text" ref="ipt"/>
 //给组件使用
    <comp-detail ref="component"></list-detail>
    <button @click="confirm">确定</button>
</template>
methods:{
    confirm(){
        console.log(this.$refs.ipt.value)  //打印出输入框中的value值
        this.$refs['component'].init()     //调用组件comp-detail中的init()方法
     }
}
posted @ 2022-07-14 14:23  又一岁荣枯  阅读(1490)  评论(0)    收藏  举报