详细介绍Vue.js中ref($refs)用法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了详细介绍Vue.js中ref($refs)用法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2674字,纯文字阅读大概需要4分钟。
内容图文
![详细介绍Vue.js中ref($refs)用法](/upload/InfoBanner/zyjiaocheng/287/aad2fa5a14854340932428a41f29bad4.jpg)
本文介绍了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>我是子组件</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>我是子组件</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 ref='insideDomRef'>我是子组件</h5>" + "</p>", methods:{ consoleRef:function () { console.log(this); // p.childComp vue实例 console.log(this.$refs.insideDomRef); // <h5 >我是子组件</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如何设置多个Class
用JQuery如何实现表单验证,具体应该怎么做?
使用Angular如何实现国际化(详细教程)
通过nodejs使用http模块发送请求(详细教程)
以上就是详细介绍Vue.js中ref ($refs)用法的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的详细介绍Vue.js中ref($refs)用法全部内容,希望文章能够帮你解决详细介绍Vue.js中ref($refs)用法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。