Vue.js $refs用法案例详解
更新时间:2021年09月14日 10:09:10 作者:猫老板的豆
这篇文章主要介绍了Vue.js $refs用法案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
尽管有 prop 和事件,但是有时仍然需要在 JavaScript 中直接访问子组件。为此可以使用 ref 为子组件指定一个引用 ID。
ref 为子组件指定一个引用 ID,使父组件能通过 ref 直接访问子组件中的数据
通过 this.$refs.outsideComponentRef 能直接定位到 ref=“outsideComponentRef” 的上,并返回该实例化对象
一、ref使用在外面的组件上
<div id="app"> <component-father ref="outsideComponentRef"></component-father> </div> <script> var refoutsidecomponentTem = { template: "<div class='childComp'><h5>{{test}}</h5></div>", data(){ return{ test:'我是子组件' } } }; new Vue({ el: "#app", components: { "component-father": refoutsidecomponentTem }, mounted:function () { console.log(this); // #app vue实例 console.log(this.$refs.outsideComponentRef); // VueComponent vue实例 console.log(this.$refs.outsideComponentRef.test); // '我是子组件' } }); </script>
二、ref使用在外面的元素上
<div id="app"> <component-father></component-father> <p ref="outsideComponentRef">p标签</p> </div> <script> var refoutsidecomponentTem = { template: "<div class='childComp'><h5>{{test}}</h5></div>", data(){ return{ test:'我是子组件' } } }; new Vue({ el: "#app", components: { "component-father": refoutsidecomponentTem }, mounted:function () { console.log(this.$refs.outsideComponentRef); // 返回 “<p>p标签</p>”对象 } }); </script>
到此这篇关于Vue.js $refs用法案例详解的文章就介绍到这了,更多相关Vue.js $refs用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Nuxt.js nuxt-link与router-link的区别说明
这篇文章主要介绍了Nuxt.js nuxt-link与router-link的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-11-11vue+elementUI 复杂表单的验证、数据提交方案问题
这篇文章主要介绍了vue+elementUI 复杂表单的验证、数据提交方案,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-06-06
最新评论