• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

浅谈Vue.js中ref ($refs)用法举例总结

vue 搞代码 4年前 (2022-01-08) 19次浏览 已收录 0个评论

本篇文章主要介绍了浅谈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搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:浅谈Vue.js中ref ($refs)用法举例总结

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址