vue里ref ($refs)用法


ref 有三種用法:

  1、ref 加在普通的元素上,用this.ref.name 獲取到的是dom元素

  2、ref 加在子組件上,用this.ref.name 獲取到的是組件實例,可以使用組件的所有方法

  3、如何利用 v-for 和 ref 獲取一組數組或者dom 節點

  注意:

  1、ref 需要在dom渲染完成后才會有,在使用的時候確保dom已經渲染完成。比如在生命周期 mounted(){} 鈎子中調用,或者在 this.$nextTick(()=>{}) 中調用

  2、如果ref 是循環出來的,有多個重名,那么ref的值會是一個數組 ,此時要拿到單個的ref 只需要循環就可以了。

一、ref使用在外面的組件上

復制代碼
<div id="ref-outside-component" v-on:click="consoleRef">
    <component-father ref="outsideComponentRef">
    </component-father>
    <p>ref在外面的組件上</p>
</div>

    var refoutsidecomponentTem={
        template:"<div class='childComp'><h5>我是子組件</h5></div>"
    };
    var refoutsidecomponent=new Vue({
        el:"#ref-outside-component",
        components:{
            "component-father":refoutsidecomponentTem
        },
        methods:{
            consoleRef:function () {
                console.log(this); // #ref-outside-component     vue實例
                console.log(this.$refs.outsideComponentRef);  // div.childComp vue實例,組件實例
            }
        }
    });
復制代碼

二、ref作用在外面元素上

復制代碼
//ref在外面的元素上
<div id="ref-outside-dom" v-on:click="consoleRef" >
   <component-father>
   </component-father>
   <p ref="outsideDomRef">ref在外面的元素上</p>
</div>

    var refoutsidedomTem={
        template:"<div class='childComp'><h5>我是子組件</h5></div>"
    };
    var refoutsidedom=new Vue({
        el:"#ref-outside-dom",
        components:{
            "component-father":refoutsidedomTem
        },
        methods:{
            consoleRef:function () {
                console.log(this); // #ref-outside-dom    vue實例
                console.log(this.$refs.outsideDomRef);  //  <p>標簽dom元素 ref在外面的元素上</p>
            }
        }
    });
復制代碼

三、ref使用在里面的元素上--局部注冊組件

復制代碼
//ref在里面的元素上
<div id="ref-inside-dom">
    <component-father>
    </component-father>
    <p>ref在里面的元素上</p>
</div>

    var refinsidedomTem={
        template:"<div class='childComp' v-on:click='consoleRef'>" +
                       "<h5 ref='insideDomRef'>我是子組件</h5>" +
                  "</div>",
        methods:{
            consoleRef:function () {
                console.log(this);  // div.childComp   vue實例 
                console.log(this.$refs.insideDomRef);  // <h5 >我是子組件</h5>
            }
        }
    };
    var refinsidedom=new Vue({
        el:"#ref-inside-dom",
        components:{
            "component-father":refinsidedomTem
        }
    });
復制代碼

四、ref使用在里面的元素上--全局注冊組件

復制代碼
//ref在里面的元素上--全局注冊
<div id="ref-inside-dom-all">
    <ref-inside-dom-quanjv></ref-inside-dom-quanjv>
</div>

    Vue.component("ref-inside-dom-quanjv",{
        template:"<div class='insideFather'> " +
                    "<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" +
                    "  <p>ref在里面的元素上--全局注冊 </p> " +
                  "</div>",
        methods:{
            showinsideDomRef:function () {
                console.log(this); //這里的this其實還是div.insideFather
                console.log(this.$refs.insideDomRefAll); // <input  type="text">
            }
        }
    });

    var refinsidedomall=new Vue({
        el:"#ref-inside-dom-all"
    });


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2024 CODEPRJ.COM