脚本宝典收集整理的这篇文章主要介绍了Vue.js $refs用法案例详解,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
尽管有 PRop 和事件,但是有时仍然需要在 JavaScript 中直接访问子组件。为此可以使用 ref 为子组件指定一个引用 ID。
ref 为子组件指定一个引用 ID,使父组件能通过 ref 直接访问子组件中的数据
通过 this.$refs.outsidecomponentRef 能直接定位到 ref=“outsideComponentRef” 的上,并返回该实例化对象
<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>
<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用法内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本宝典!
以上是脚本宝典为你收集整理的Vue.js $refs用法案例详解全部内容,希望文章能够帮你解决Vue.js $refs用法案例详解所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。