Vue.js中ref ($refs)用法舉例總結
阿新 • • 發佈:2018-12-15
原文地址:http://www.cnblogs.com/xueweijie/p/6907676.html
<div id="app"> <input type="text" ref="input1"/> <button @click="add">新增</button> </div>
<script> new Vue({ el: "#app", methods:{ add:function(){ this.$refs.input1.value ="22"; //this.$refs.input1 減少獲取dom節點的消耗 } } }) </script>
一般來講,獲取DOM元素,需document.querySelector(".input1")獲取這個dom節點,然後在獲取input1的值。
但是用ref繫結之後,我們就不需要在獲取dom節點了,直接在上面的input上繫結input1,然後$refs裡面呼叫就行。
然後在javascript裡面這樣呼叫:this.$refs.input1 這樣就可以減少獲取dom節點的消耗了
以下內容:
作者:該帳號已被查封
連結:http://www.jianshu.com/p/3bd8a2b07d57
來源:簡書
看Vue.js文件中的ref部分,自己總結了下ref的使用方法以便後面查閱。
一、ref使用在外面的元件上
HTML 部分
1 <div id="ref-outside-component" v-on:click="consoleRef"> 2 <component-father ref="outsideComponentRef"> 3 </component-father> 4 <p>ref在外面的元件上</p> 5 </div>
js部分
1 var refoutsidecomponentTem={ 2 template:"<div class='childComp'><h5>我是子元件</h5></div>" 3 }; 4 var refoutsidecomponent=new Vue({ 5 el:"#ref-outside-component", 6 components:{ 7 "component-father":refoutsidecomponentTem 8 }, 9 methods:{ 10 consoleRef:function () { 11 console.log(this); // #ref-outside-component vue例項 12 console.log(this.$refs.outsideComponentRef); // div.childComp vue例項 13 } 14 } 15 });
二、ref使用在外面的元素上
HTML部分
1 <!--ref在外面的元素上--> 2 <div id="ref-outside-dom" v-on:click="consoleRef" > 3 <component-father> 4 </component-father> 5 <p ref="outsideDomRef">ref在外面的元素上</p> 6 </div>
JS部分
1 var refoutsidedomTem={ 2 template:"<div class='childComp'><h5>我是子元件</h5></div>" 3 }; 4 var refoutsidedom=new Vue({ 5 el:"#ref-outside-dom", 6 components:{ 7 "component-father":refoutsidedomTem 8 }, 9 methods:{ 10 consoleRef:function () { 11 console.log(this); // #ref-outside-dom vue例項 12 console.log(this.$refs.outsideDomRef); // <p> ref在外面的元素上</p> 13 } 14 } 15 });
三、ref使用在裡面的元素上---區域性註冊元件
HTML部分
1 <!--ref在裡面的元素上--> 2 <div id="ref-inside-dom"> 3 <component-father> 4 </component-father> 5 <p>ref在裡面的元素上</p> 6 </div>
JS部分
1 var refinsidedomTem={ 2 template:"<div class='childComp' v-on:click='consoleRef'>" + 3 "<h5 ref='insideDomRef'>我是子元件</h5>" + 4 "</div>", 5 methods:{ 6 consoleRef:function () { 7 console.log(this); // div.childComp vue例項 8 console.log(this.$refs.insideDomRef); // <h5 >我是子元件</h5> 9 } 10 } 11 }; 12 var refinsidedom=new Vue({ 13 el:"#ref-inside-dom", 14 components:{ 15 "component-father":refinsidedomTem 16 } 17 });
四、ref使用在裡面的元素上---全域性註冊元件
HTML部分
1 <!--ref在裡面的元素上--全域性註冊--> 2 <div id="ref-inside-dom-all"> 3 <ref-inside-dom-quanjv></ref-inside-dom-quanjv> 4 </div>
JS部分
1 Vue.component("ref-inside-dom-quanjv",{ 2 template:"<div class='insideFather'> " + 3 "<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" + 4 " <p>ref在裡面的元素上--全域性註冊 </p> " + 5 "</div>", 6 methods:{ 7 showinsideDomRef:function () { 8 console.log(this); //這裡的this其實還是div.insideFather 9 console.log(this.$refs.insideDomRefAll); // <input type="text"> 10 } 11 } 12 }); 13
14 var refinsidedomall=new Vue({ 15 el:"#ref-inside-dom-all" 16 });
<div id="app"> <input type="text" ref="input1"/> <button @click="add">新增</button> </div>
<script> new Vue({ el: "#app", methods:{ add:function(){ this.$refs.input1.value ="22"; //this.$refs.input1 減少獲取dom節點的消耗 } } }) </script>
一般來講,獲取DOM元素,需document.querySelector(".input1")獲取這個dom節點,然後在獲取input1的值。
但是用ref繫結之後,我們就不需要在獲取dom節點了,直接在上面的input上繫結input1,然後$refs裡面呼叫就行。
然後在javascript裡面這樣呼叫:this.$refs.input1 這樣就可以減少獲取dom節點的消耗了