Vue 中的 ref $refs

ref 被用來給DOM元素或子組件註冊引用信息。引用信息會根據父組件的 $refs 對象進行註冊。若是在普通的DOM元素上使用,引用信息就是元素; 若是用在子組件上,引用信息就是組件實例

注意:只要想要在Vue中直接操做DOM元素,就必須用ref屬性進行註冊

這裏寫圖片描述

實例:

這裏寫圖片描述

這裏寫圖片描述

這裏爲了在create的時候引用DOM元素,先在DOM中使用ref標籤進行了註冊,而後即可以經過’this.$refs’再跟註冊時的名稱來引用DOM元素了html

第二部分vue

 

vue中的 ref 和 $refs

 

如圖,ref 被用來給元素或子組件註冊引用信息。引用信息將會註冊在父組件的 $refs 對象上。若是在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;若是用在子組件上,引用就指向組件實例:dom

在上面的例子中,input的引用信息爲input1 ,$refs 是全部註冊過的ref的一個集合,ide

console.log(this.$refs.input1)//<input type="text" id="input1">
console.log(document.getElementById('input1'))//<input type="text" id="input1">post

 這兩種方法得到的都是Dom節點,而$refs相對document.getElementById的方法,會減小獲取dom節點的消耗。ui

ref和v-for在一塊兒的狀況this

li裏的ref的沒法讀取item裏面的值,即item.name或被直接讀取爲字符串「item.name」,spa

此時的$refs3d

 第三部分:code

 

1、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>
  6.  
     

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.  
    });
  16.  
     

2、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.  
    });
  16.  
     

3、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.  
    });
  18.  
     

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