ref definition: used to register reference information for an element or subcomponent. The reference information will be registered on the $refs object on the parent component.
Example: Component 1: <template> <div> I am{ {name}} </div> </template> <script> export default { name:'Cpn1', data() { return { name:'Component 1' } }, } </script> Component 2: <template> <div>I am { {name}}</div> </template> <script> export default { name:'Cpn2', data() { return { name:'Component 2' } }, } </script> App.vue <template> <div id="app"> <cpn-1 ref="c1"></cpn-1> <cpn-2 ref="c2"></cpn-2> <button @click="showDom">Button</button> <h2 ref="title">I am the title</h2> <input type="text" ref="input" value="123"> </div> </template> <script> import Cpn1 from "./components/Cpn1.vue"; import Cpn2 from "./components/Cpn2.vue"; export default { components: Cpn1, Cpn2 }, name: "App", methods: { showDom() { console.log(this.$refs.c1); console.log(this.$refs.c2.$data.name); console.log(this.$refs.title) console.log(this.$refs.input.value) // Get a real DOM object and modify the value var title = this.$refs.title; title.innerText="helloWord" }, }, }; </script> Execute the above program and click the "Button" on the page. The effect is as follows: Also look at the console: You can see that when the ref object is used on a normal element, a normal DOM element is obtained. When ref is used on a child component, the reference points to the component instance. According to actual needs, we can register reference information for elements or subcomponents through ref. When needed, we can use $refs to obtain the real DOM element or component instance to perform the operations we want. This is the end of this article about the usage and demonstration of ref in Vue. For more information about the usage of ref in Vue, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: MySQL 8.0.25 installation and configuration method graphic tutorial
Preface I am a PHP programmer who started out as ...
After installing Redis on Linux, use Java to conn...
Without further ado, I will post the code for you...
The previous article wrote about how to manually ...
1. Trash or Classic? Web technology updates very ...
This article describes how to install and configu...
Table of contents 1. Page Rendering 2. Switch tag...
Table of contents Basic instructions and usage An...
Solution to the problem that there is no unzip co...
My machine environment: Windows 2008 R2 MySQL 5.6...
When developing applications that use a database,...
The effect is as follows: Example 1 Example 2: Ta...
1. Install shadowsocks sudo apt-get install pytho...
I have always wanted to learn about caching. Afte...
There are three types of MySQL stored procedure p...