Steps for Vue to use Ref to get components across levels

Steps for Vue to use Ref to get components across levels

Vue uses Ref to get component instances across levels

Example Introduction

During the development process, we will inevitably use cross-level ref instance acquisition. In most cases, we can find the required instance through the component's own parent or children . However, when the hierarchy is unclear or too deep, this method is inevitably cumbersome and inefficient.

As shown in the figure below, we use組件E to obtain the component instance of組件D

Document directory structure

There are six components, namely A, B, C, D, E and index, and they are inserted into their respective pages in the order of the components in the above figure.

The page style is as follows:

Install vue-ref

Download vue-ref

npm install vue-ref --save

Global Registration

import ref from 'vue-ref'
Vue.use(ref)

How to use

<!-- vm.dom will be the DOM node -->
<p v-ref="c => this.dom = c">hello</p>

<!-- vm.child will be the child component instance -->
<child-component v-ref="c => this.child = c"></child-component>

<span v-for="n in 10" :key="n" v-ref="(c, key) => {...}">{{ n }} </span>

Root component custom method [using provide and inject]

In our index page, we provide three methods:

  • Set the instance of the child component, setChildrenRef
  • Get from component instance, getChildrenRef
  • Get the current node instance, getRef
provide() {
  return {
   setChildrenRef: (name, ref) => {
    this[name] = ref
   },
   getChildrenRef: name => {
    return this[name]
   },
   getRef: () => {
    return this
   }
  }
 },

Describe each page separately

Component A page:

Get the setChildrenRef method through the injection method, and cache component D through the above instructions

Component B page:

Component C page:

Component D page:

Component E page:

In this page, we not only inject two methods, but also set a method to switch the color of component D to test whether we have really obtained the instance of component D across levels.

result

As you can see, the three parent instances are the same, and the text style of component D is successfully modified in component E. good!

The above is the details of the steps for Vue to use Ref to obtain components across levels. For more information about Vue using Ref to obtain components, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • In-depth explanation of Vue multi-select list component
  • Problems and solutions encountered when using v-model to two-way bind the values ​​of parent-child components in Vue
  • Vue example code using transition component animation effect
  • Summary of Vue component basics
  • In-depth understanding of Vue dynamic components and asynchronous components
  • Vue implements multi-tab component
  • Easily implement the whole process of switch function components in vue3
  • How to customize dialog and modal components in Vue3
  • Correct way to force component to re-render in Vue
  • Two ways to dynamically create components in Vue
  • Detailed explanation of incompatible changes of components in vue3

<<:  Installation tutorial of the latest stable version of MySQL 5.7.17 under Linux

>>:  mysql5.7 create user authorization delete user revoke authorization

Recommend

Steps to encapsulate the carousel component in vue3.0

Table of contents 1: Encapsulation idea 2. Packag...

Linux implements the source code of the number guessing game

A simple Linux guessing game source code Game rul...

Master-slave synchronization configuration of Mysql database

Table of contents Mysql master-slave synchronizat...

Solution to the problem of repeated pop-up of Element's Message pop-up window

Table of contents 1. Use 2. Solve the problem of ...

Summary of the differences and usage of plugins and components in Vue

The operating environment of this tutorial: Windo...

How to install PostgreSQL and PostGIS using yum on CentOS7

1. Update the yum source The PostgreSQL version o...

Use Docker to run multiple PHP versions on the server

PHP7 has been out for quite some time, and it is ...

MySQL 8.0.11 MacOS 10.13 installation and configuration method graphic tutorial

The process of installing MySQL database and conf...

Apache Spark 2.0 jobs take a long time to finish when they are finished

Phenomenon When using Apache Spark 2.x, you may e...

Detailed explanation of jQuery method attributes

Table of contents 1. Introduction to jQuery 2. jQ...

Pure CSS drop-down menu

Achieve results Implementation Code html <div ...

js uses FileReader to read local files or blobs

Table of contents FileReader reads local files or...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...