Introduction and usage examples of ref and $refs in Vue

Introduction and usage examples of ref and $refs in Vue

Preface

In 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 Introduction

ref 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 Introduction

this.$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>

Summarize

This 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:
  • A brief exploration of $refs in Vue
  • Vue solves the problem of getting DOM or component errors through this.$refs
  • Vue single file component cannot get $refs problem
  • Vue parent component obtains the value and method of child component through $refs
  • Analysis of the usage of ref and $refs in vue.js
  • Notes on using $refs in Vue instances

<<:  JS addEventListener() and attachEvent() methods implement registration events

>>:  js dynamically generates tables (node ​​operations)

Recommend

JavaScript selector functions querySelector and querySelectorAll

Table of contents 1. querySelector queries a sing...

XHTML three document type declarations

XHTML defines three document type declarations. T...

Detailed explanation of the correct use of the if function in MySQL

For what I am going to write today, the program r...

A brief discussion on MySQL select optimization solution

Table of contents Examples from real life Slow qu...

MySQL 5.5.27 installation graphic tutorial

1. Installation of MYSQL 1. Open the downloaded M...

Adobe Brackets simple use graphic tutorial

Adobe Brackets is an open source, simple and powe...

How to implement concurrency control in JavaScript

Table of contents 1. Introduction to Concurrency ...

Win32 MySQL 5.7.27 installation and configuration method graphic tutorial

The installation tutorial of MySQL 5.7.27 is reco...

A brief discussion on DDL and DML in MySQL

Table of contents Preface 1. DDL 1.1 Database Ope...

Nodejs combined with Socket.IO to realize websocket instant communication

Table of contents Why use websocket Socket.io Ope...

Detailed explanation of Vue's sync modifier

Table of contents 1. Instructions 2. Modifiers 3....

jQuery implements form validation function

jQuery form validation example / including userna...

Detailed example of concatenating multiple fields in mysql

The MySQL query result row field splicing can be ...

Talk about the understanding of CSS attribute margin

1.What is margin? Margin is used to control the sp...

Why do code standards require SQL statements not to have too many joins?

Free points Interviewer : Have you ever used Linu...