浅谈Vue.js中ref ($refs)用法举例总结
网络编程 2021-07-04 17:33www.168986.cn编程入门
本篇文章主要介绍了浅谈Vue.js中ref ($refs)用法举例,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
本文介绍了Vue.js中ref ($refs)用法举例,分享给大家,具体如下
看Vue.js文档中的ref部分,自己了下ref的使用方法以便后面查阅。
一、ref使用在外面的组件上
HTML 部分
<div id="ref-outside-ponent" v-on:click="consoleRef"> <ponent-father ref="outsideComponentRef"> </ponent-father> <p>ref在外面的组件上</p> </div>
js部分
var refoutsideponentTem={ template:"<div class='childComp'><h5>我是子组件</h5></div>" }; var refoutsideponent=new Vue({ el:"#ref-outside-ponent", ponents:{ "ponent-father":refoutsideponentTem }, methods:{ consoleRef:function () { console.log(this); // #ref-outside-ponent vue实例 console.log(this.$refs.outsideComponentRef); // div.childComp vue实例 } } });
二、ref使用在外面的元素上
HTML部分
<!--ref在外面的元素上--> <div id="ref-outside-dom" v-on:click="consoleRef" > <ponent-father> </ponent-father> <p ref="outsideDomRef">ref在外面的元素上</p> </div>
JS部分
var refoutsidedomTem={ template:"<div class='childComp'><h5>我是子组件</h5></div>" }; var refoutsidedom=new Vue({ el:"#ref-outside-dom", ponents:{ "ponent-father":refoutsidedomTem }, methods:{ consoleRef:function () { console.log(this); // #ref-outside-dom vue实例 console.log(this.$refs.outsideDomRef); // <p> ref在外面的元素上</p> } } });
三、ref使用在里面的元素上---局部注册组件
HTML部分
<!--ref在里面的元素上--> <div id="ref-inside-dom"> <ponent-father> </ponent-father> <p>ref在里面的元素上</p> </div>
JS部分
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", ponents:{ "ponent-father":refinsidedomTem } });
四、ref使用在里面的元素上---全局注册组件
HTML部分
<!--ref在里面的元素上--全局注册--> <div id="ref-inside-dom-all"> <ref-inside-dom-quanjv></ref-inside-dom-quanjv> </div>
JS部分
Vue.ponent("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" });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指