$refs的用法及作用

获取DOM元素,一般用document.querySelector获取这个dom节点,然后在获取input的值

但是用ref绑定之后,就不需要在获取dom节点了,直接在上面的input上绑定input1,然后$refs里面调用即可

this.$refs.input1  这样可以减少获取DOM节点的消耗

<div id="app">
    <input type="text" ref="input1"/>
    <button @click="add">添加</button>
</div>
new Vue({
    el: "#app",
    methods:{
    add:function(){
         //this.$refs.input1  减少获取dom节点的消耗
        this.$refs.input1.value ="996";
        }
    }
})

 

posted @ 2019-04-20 19:01  紫诺花开  阅读(3532)  评论(0编辑  收藏  举报