this.$refs 用于 vue 获取 dom元素

2019-08-27  本文已影响0人  Mr菜头

vue $refs 基本用法:返回 一个 对象 ,包括注册过 【ref 特性】的 所有dom元素 和 组件实例。

<!-- `vm.$refs.p` will be the DOM node -->
<p ref="p">hello</p>

<!-- `vm.$refs.child` will be the child component instance -->
<child-component ref="child"></child-component>

调用列子:

<input ref="input1" ></input>
<button @click="add"></button>
new Vue({
    el: "#app",
    methods:{
    add:function(){
        this.$refs.input1.value ="22"; //this.$refs.input1  减少获取dom节点的消耗
        }
    }
})
上一篇 下一篇

猜你喜欢

热点阅读