本篇文章主要介绍了浅谈Vue.js中ref ($refs)用法举例总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本文介绍了Vue.js中ref ($refs)用法举例总结,分享给大家,具体如下:
看Vue.js文档中的ref部分,自己总结了下ref的使用方法以便后面查阅。
一、ref使用在外面的组件上
HTML 部分
<div id="ref-outside-component"> <p>ref在外面的组件上</p></div>
js部分
var refoutsidecomponentTem={ template:"<div class='childComp'><h5>我是子组件</h5></div>" }; var refoutsidecomponent=new Vue({ el:<b style="color:transparent">本文来源gao@!dai!ma.com搞$$代^@码!网!</b>"#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使用在外面的元素上
HTML部分
<!--ref在外面的元素上--><div id="ref-outside-dom"> <p>ref在外面的元素上</p></div>
JS部分
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> ref在外面的元素上</p> } } });
三、ref使用在里面的元素上—局部注册组件
HTML部分
<!--ref在里面的元素上--><div id="ref-inside-dom"> <p>ref在里面的元素上</p></div>
JS部分
var refinsidedomTem={ template:"<div class='childComp'>" + "<h5>我是子组件</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使用在里面的元素上—全局注册组件
HTML部分
<!--ref在里面的元素上-全局注册--><div id="ref-inside-dom-all"> </div>
JS部分
Vue.component("ref-inside-dom-quanjv",{ template:"<div class='insideFather'> " + "" + " <p>ref在里面的元素上--全局注册 </p> " + "</div>", methods:{ showinsideDomRef:function () { console.log(this); //这里的this其实还是div.insideFather console.log(this.$refs.insideDomRefAll); // } } }); var refinsidedomall=new Vue({ el:"#ref-inside-dom-all" });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网。
以上就是浅谈Vue.js中ref ($refs)用法举例总结的详细内容,更多请关注gaodaima搞代码网其它相关文章!