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"></comp-detail>
    <button @click="confirm">确定</button>
</template>
methods:{
    confirm(){
        console.log(this.$refs.ipt.value)  //打印出输入框中的value值
        this.$refs['component'].init()     //调用组件comp-detail中的init()方法
     }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐