Despite props and events, sometimes you still need to access child components directly in JavaScript. To do this, you can use ref to assign a reference ID to the child component. ref specifies a reference ID for the child component, so that the parent component can directly access the data in the child component through ref This.$refs.outsideComponentRef can directly locate ref="outsideComponentRef" and return the instantiated object 1. Ref is used on external components<div id="app"> <component-father ref="outsideComponentRef"></component-father> </div> <script> var refoutsidecomponentTem = { template: "<div class='childComp'><h5>{{test}}</h5></div>", data(){ return { test:'I am a child component' } } }; new Vue({ el: "#app", components: "component-father": refoutsidecomponentTem }, mounted:function () { console.log(this); // #app vue instance console.log(this.$refs.outsideComponentRef); // VueComponent vue instance console.log(this.$refs.outsideComponentRef.test); // 'I am a child component' } }); </script> 2. Ref is used on external elements<div id="app"> <component-father></component-father> <p ref="outsideComponentRef">p tag</p> </div> <script> var refoutsidecomponentTem = { template: "<div class='childComp'><h5>{{test}}</h5></div>", data(){ return { test:'I am a child component' } } }; new Vue({ el: "#app", components: "component-father": refoutsidecomponentTem }, mounted:function () { console.log(this.$refs.outsideComponentRef); // Returns "<p>p标签</p>" object} }); </script> This is the end of this article about the detailed case study of Vue.js $refs usage. For more relevant Vue.js $refs usage content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation of installing and uninstalling CUDA and CUDNN in Ubuntu
>>: Detailed analysis of the principles and usage of MySQL views
First of all, what is a font icon? On the surface...
<body> <div id="root"> <...
Table of contents 1. Panorama II. Background 1. R...
Vue implements the palace grid rotation lottery (...
MySQL database basic syntax DDL Operations Create...
Table of contents 1. Problem 2. Solution Option 1...
1. Docker Network Mode When docker run creates a ...
In CSS, text is one of the most common things we ...
After watching this, I guarantee that you have ha...
mysql efficient query MySQL sacrifices group by t...
Click here to return to the 123WORDPRESS.COM HTML ...
MySQL handles duplicate data Some MySQL tables ma...
vertical-align attribute is mainly used to change...
Using fonts on the Web is both a fundamental skill...
This article introduces Docker+Jenkins automatic ...