Home Web Front-end JS Tutorial Detailed explanation of using refs to locate dom in vue and undefined appears

Detailed explanation of using refs to locate dom in vue and undefined appears

Dec 22, 2017 am 09:10 AM
refs Appear position

I have always felt that it is very convenient to use ref to locate dom nodes. But I encountered a problem during the period, that is, when this.$refs.xxx is used in the mounted(){} hook, what is printed is undefined?

So I compared the .vue files that were previously positioned using ref and found the difference between them. This article mainly introduces the solution to the problem of using refs to locate undefined in DOM in Vue. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

If we want to know why a certain DOM node cannot be located, we must first understand what the mounted(){} hook function is used for.

The following is the vue life cycle (part) given by vue official. As the official said, you don’t need to understand it at the beginning, but as you learn and use it, its reference value will become more and more high.

It turns out that in the mounted stage, the DOM structure is ready, but the readiness here needs special explanation:

The DOM structure has come out, but if If a certain DOM node in the DOM structure uses v-if, v-show or v-for (that is, dynamically operating the DOM based on the obtained background data, that is, responsive), then these DOMs will not be found in the mounted stage.

The mounted stage at this time is generally used to initiate back-end requests, get back data, and do something with the routing hook. To put it simply, it is just loading data in the mounted hook. The loaded data is not

will be updated in the DOM at this stage, so if $refs is used in the mounted hook, if the ref is located in a DOM node with v-if, v-for, v-show, it will be returned They can only be undefined, because they do not exist at all in the mounted stage! !

After inspection, the text above is wrong. The main reason why $refs cannot be located is because if statements such as v-if, v-for, and v-show rely on parameters passed by the parent component, the This parameter has not been obtained during the mounted() stage~~~~! ! ! !

If you want to really get the data after the DOM is loaded, you need to call VUE's global api: this.$nextTick(() => {})

If you say mounted The stage is the loading stage, then the updated stage is the stage where the data is updated to the DOM (the loaded data is processed). At this time, ref, data, etc. are all mounted on the DOM structure. Use this in the update stage. .$refs.xxx, the DOM node can be found 100% of the time.

The difference between updated and mounted is that vue will call the updated(){} hook function every time the DOM structure is updated! Mounted is only executed once. To put it simply, as long as you can see the existence of the element during debugging, you can use this.$refs.xxx to find the corresponding DOM node in the updated stage!

Regarding the use of $refs, the official documentation specifically gives the following tips:

Be careful when using it.

Related recommendations:

Detailed explanation of the difference between undefined and null in JavaScript

Solution to php prompt for undefined index

Solution to the problem of Call to undefined function curl_init() when running php

The above is the detailed content of Detailed explanation of using refs to locate dom in vue and undefined appears. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which one is better? What are the differences, advantages and disadvantages of NTFS and ReFS in Windows 11? Which one is better? What are the differences, advantages and disadvantages of NTFS and ReFS in Windows 11? May 10, 2023 am 08:06 AM

NTFS has always been the default file system for Windows PCs, but Microsoft is introducing some juicy stuff with ReFS. This new file system may be coming to Windows 11, and users are eager to know how NTFS will compare to ReFS. In this detailed guide, we will show you the better choice between these two file systems. Does Windows 11 support ReFS? Discussions about Windows 11 supporting ReFS (Resilient File System) are still tentative and ongoing. This file system is not officially available on Windows 11 PCs yet. However, DevsChannel and WindowsServe

How to locate Apple wireless earphones if they are lost_How to locate Apple wireless earphones How to locate Apple wireless earphones if they are lost_How to locate Apple wireless earphones Mar 23, 2024 am 08:21 AM

1. First, we open the [Search] App on the mobile phone and select the device in the list on the device interface. 2. Then, you can check the location and click on the route to navigate there.

How to use map and location functions in uniapp How to use map and location functions in uniapp Oct 16, 2023 am 08:01 AM

How to use map and positioning functions in uniapp 1. Background introduction With the popularity of mobile applications and the rapid development of positioning technology, map and positioning functions have become an indispensable part of modern mobile applications. uniapp is a cross-platform application development framework developed based on Vue.js, which can facilitate developers to share code on multiple platforms. This article will introduce how to use maps and positioning functions in uniapp and provide specific code examples. 2. Use the uniapp-amap component to implement the map function

How to use a WordPress plugin to implement instant location functionality How to use a WordPress plugin to implement instant location functionality Sep 05, 2023 pm 04:51 PM

How to use WordPress plug-ins to achieve instant location functionality With the popularity of mobile devices, more and more websites are beginning to provide geolocation-based services. In WordPress websites, we can use plug-ins to implement instant positioning functions and provide visitors with services related to their geographical location. 1. Choose the right plug-in. There are many plug-ins that provide geolocation services in the WordPress plug-in library to choose from. Depending on the needs and requirements, choosing the right plug-in is the key to achieving instant positioning functionality. Here are a few

How to locate the other party's mobile phone location on Amap - How to locate the other party's mobile phone location on Amap How to locate the other party's mobile phone location on Amap - How to locate the other party's mobile phone location on Amap Apr 01, 2024 pm 02:11 PM

1. Click to enter the Amap map software on your mobile phone. 2. Click My in the lower right corner. 3. Click to enter the family map. 4. Click Create My Family Map. 5. After the creation is successful, an invitation code will appear and can be shared with another mobile phone.

Methods to solve the problem of memory leak location in Go language development Methods to solve the problem of memory leak location in Go language development Jul 01, 2023 pm 12:33 PM

Methods to solve the problem of memory leak location in Go language development: Memory leak is one of the common problems in program development. In Go language development, due to the existence of its automatic garbage collection mechanism, memory leak problems may be less than other languages. However, when we face large and complex applications, memory leaks may still occur. This article will introduce some common methods to locate and solve memory leak problems in Go language development. First, we need to understand what a memory leak is. Simply put, a memory leak refers to the

How to change the location information and how to modify the address How to change the location information and how to modify the address Mar 12, 2024 pm 09:52 PM

We all know very clearly that Taku APP is a very reliable chat and social platform. Now it allows everyone to make friends online. Some of the forms of making friends here mainly allow people to make friends by location. Oh, it's so simple and direct. After all, it can automatically locate your current location information for you, and better match you with some friends in the same city who are close to each other, so that everyone can chat more easily and feel special. Happy, many times, in order to get to know more friends in other places, everyone has the idea of ​​​​modifying their address, but they don’t know how to modify their location information, which is very difficult. troubled, so the editor of this site also collected some specific

How to quickly find the location of a Huawei phone after it is lost? How to quickly find the location of a Huawei phone after it is lost? Mar 24, 2024 am 08:48 AM

In today's society, mobile phones have become an indispensable part of our lives. As a well-known smartphone brand, Huawei mobile phones are deeply loved by users. However, with the popularity of mobile phones and the increase in frequency of use, mobile phones are often lost. Once our phone is lost, we tend to feel anxious and confused. So, if you unfortunately lose your Huawei phone, how can you quickly find its location? Step 1: Use the mobile phone positioning function. Huawei mobile phones have built-in powerful positioning functions. Users can use the "Security" option in the mobile phone settings.

See all articles