PrefaceIn JavaScript, you need to use document.querySelector("#demo") to get the DOM node, and then get the value of this node. In Vue, we don’t need to get the DOM node. After the element is bound to ref, it can be called directly through this.$refs, which can reduce the consumption of getting the DOM node. ref Introductionref is used to register reference information for an element or subcomponent. The reference information will be registered on the $refs object of the parent component. If used on a normal DOM element, the reference points to the DOM element; if used on a subcomponent, the reference points to the subcomponent instance. In layman's terms, the ref feature is to assign an ID reference to an element or subcomponent, and access the instance of the element or subcomponent through this.$refs.refName <p ref="p">Hello</p> <children ref="children"></children> this.$refs.p this.$refs.children this.$refs Introductionthis.$refs is an object that holds all DOM elements and subcomponent instances that have registered ref attributes in the current component Note: $refs are only populated after the component is rendered, you cannot access them during the initial render, and it is non-responsive, so you cannot use it for data binding in templates Notice: When ref is used with v-for, the reference obtained will be an array containing the loop array source <template> <div> <div ref="myDiv" v-for="(item, index) in arr" :key="index">{{item}}</div> </div> </template> <script> export default { data() { return { arr: ['one', 'two', 'three', 'four'] } }, mounted() { console.log(this.$refs.myDiv) }, methods: {} } </script> <style lang="sass" scoped> </style> Instance (calling the method of the child component through the ref attribute)【1】Subcomponent code: <template> <div>{{msg}}</div> </template> <script> export default { data() { return { msg: 'I am a child component' } }, methods: { changeMsg() { this.msg = 'Transformation' } } } </script> <style lang="sass" scoped></style> 【2】Parent component code: <template> <div @click="parentMethod"> <children ref="children"></children> </div> </template> <script> import children from 'components/children.vue' export default { components: children }, data() { return {} }, methods: { parentMethod() { this.$refs.children //Return an object this.$refs.children.changeMsg() //Call children's changeMsg method} } } </script> <style lang="sass" scoped></style> SummarizeThis is the end of this article about the introduction and use of ref and $refs in Vue. For more information about the use of ref and $refs 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:
|
<<: JS addEventListener() and attachEvent() methods implement registration events
>>: js dynamically generates tables (node operations)
This article shares the specific code of js canva...
Table of contents 1. Create objects by literal va...
This article example shares the specific code of ...
Sometimes in our actual work, we need to import d...
Table of contents Preface Configure yum source, e...
Scenario simulation: The operation and maintenanc...
Preface: In project development, some business ta...
Pop-up windows are often used in actual developme...
In some scenarios, we need to modify our varchar ...
Nginx is a high-performance website server and re...
Table of contents 1. Introduction to NFS 2. NFS C...
This article shares the specific code of Vue to r...
The loading speed of a web page is an important in...
Install Enter the following command to install it...
This article example shares the specific code of ...