您当前的位置:首页 > 网站建设 > javascript
| php | asp | css | H5 | javascript | Mysql | Dreamweaver | Delphi | 网站维护 | 帝国cms | React | 考试系统 | ajax |

Vue.js $refs用法案例详解

51自学网 2022-05-02 21:31:36
  javascript

尽管有 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用法内容请搜索wanshiok.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持wanshiok.com!


React全局状态管理的三种底层机制探究
TypeScript学习笔记之类型窄化篇
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1