溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

vue中ref如何用

發(fā)布時(shí)間:2022-10-22 13:37:19 來(lái)源:億速云 閱讀:205 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“vue中ref如何用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“vue中ref如何用”吧!

vue中ref的用法有三種,分別是:1.ref加在普通的元素上,用this.ref.name獲取dom元素;2.ref加在子組件上,用this.ref.name 獲取組件實(shí)例,可以使用組件的所有方法;3.利用v-for和ref獲取一組數(shù)組或者dom節(jié)點(diǎn)。

示例:

1.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實(shí)例
                console.log(this.$refs.outsideComponentRef);  // div.childComp vue實(shí)例,組件實(shí)例
            }
        }
    });

2.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實(shí)例
                console.log(this.$refs.outsideDomRef);  //  <p>標(biāo)簽dom元素 ref在外面的元素上</p>
            }
        }
    });

3.ref使用在里面的元素上,局部注冊(cè)組件。

//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實(shí)例 
                console.log(this.$refs.insideDomRef);  // <h5 >我是子組件</h5>
            }
        }
    };
    var refinsidedom=new Vue({
        el:"#ref-inside-dom",
        components:{
            "component-father":refinsidedomTem
        }
    });

4.ref使用在里面的元素上,全局注冊(cè)組件。

//ref在里面的元素上--全局注冊(cè)
<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在里面的元素上--全局注冊(cè) </p> " +
                  "</div>",
        methods:{
            showinsideDomRef:function () {
                console.log(this); //這里的this其實(shí)還是div.insideFather
                console.log(this.$refs.insideDomRefAll); // <input  type="text">
            }
        }
    });
    var refinsidedomall=new Vue({
        el:"#ref-inside-dom-all"
    });

感謝各位的閱讀,以上就是“vue中ref如何用”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)vue中ref如何用這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI