這篇文章主要介紹了淺談Vue.js中ref ($refs)用法舉例總結,現在分享給大家,也給大家做個參考。
本文介紹了Vue.js中ref ($refs)用法舉例總結,分享給大家,具體如下:
看Vue.js文件中的ref部分,自己總結了下ref的使用方法以便後面查閱。
一、ref使用在外面的元件上
HTML 部分
<p id="ref-outside-component" v-on:click="consoleRef"> <component-father ref="outsideComponentRef"> </component-father> <p>ref在外面的组件上</p> </p>
js部分
var refoutsidecomponentTem={ template:"<p class='childComp'><h5 id="我是子组件">我是子组件</h5></p>" }; 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); // p.childComp vue实例 } } });
二、 ref使用在外面的元素上
HTML部分
<!--ref在外面的元素上--> <p id="ref-outside-dom" v-on:click="consoleRef" > <component-father> </component-father> <p ref="outsideDomRef">ref在外面的元素上</p> </p>
JS部分
var refoutsidedomTem={ template:"<p class='childComp'><h5 id="我是子组件">我是子组件</h5></p>" }; 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> ref在外面的元素上</p> } } });
三、ref使用在裡面的元素上---局部註冊元件
HTML部分
<!--ref在里面的元素上--> <p id="ref-inside-dom"> <component-father> </component-father> <p>ref在里面的元素上</p> </p>
JS部分
var refinsidedomTem={ template:"<p class='childComp' v-on:click='consoleRef'>" + "<h5 id="我是子组件">我是子组件</h5>" + "</p>", methods:{ consoleRef:function () { console.log(this); // p.childComp vue实例 console.log(this.$refs.insideDomRef); // <h5 id="我是子组件">我是子组件</h5> } } }; var refinsidedom=new Vue({ el:"#ref-inside-dom", components:{ "component-father":refinsidedomTem } });
四、ref使用在裡面的元素上---全域註冊元件
HTML部分
<!--ref在里面的元素上--全局注册--> <p id="ref-inside-dom-all"> <ref-inside-dom-quanjv></ref-inside-dom-quanjv> </p>
JS部分
Vue.component("ref-inside-dom-quanjv",{ template:"<p class='insideFather'> " + "<input type='text' ref='insideDomRefAll' v-on:input='showinsideDomRef'>" + " <p>ref在里面的元素上--全局注册 </p> " + "</p>", methods:{ showinsideDomRef:function () { console.log(this); //这里的this其实还是p.insideFather console.log(this.$refs.insideDomRefAll); // <input type="text"> } } }); var refinsidedomall=new Vue({ el:"#ref-inside-dom-all" });
上面是我整理給大家的,希望今後會對大家有幫助。
相關文章:
以上是詳細介紹Vue.js中ref ($refs)用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

VueUse 是 Anthony Fu 的一个开源项目,它为 Vue 开发人员提供了大量适用于 Vue 2 和 Vue 3 的基本 Composition API 实用程序函数。本篇文章就来给大家分享几个我常用的几个 VueUse 最佳组合,希望对大家有所帮助!

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

本篇文章给大家整理分享8个GitHub上很棒的的 Vue 项目,都是非常棒的项目,希望当中有您想要收藏的那一个。

如何使用VueRouter4.x?下面本篇文章就来给大家分享快速上手教程,介绍一下10分钟快速上手VueRouter4.x的方法,希望对大家有所帮助!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

記事本++7.3.1
好用且免費的程式碼編輯器

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。