$refs的用法及做用

獲取DOM元素,通常用document.querySelector獲取這個dom節點,而後在獲取input的值app

可是用ref綁定以後,就不須要在獲取dom節點了,直接在上面的input上綁定input1,而後$refs裏面調用便可dom

this.$refs.input1  這樣能夠減小獲取DOM節點的消耗this

<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";
        }
    }
})
相關文章
相關標籤/搜索