Usage and demonstration of ref in Vue

Usage and demonstration of ref in Vue

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.

  • If used on a normal dom element, the reference points to dom element;
  • If used on a child component, the reference points to the component instance.

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:
  • Detailed explanation of computed properties in Vue
  • Vue computed properties
  • setup+ref+reactive implements vue3 responsiveness
  • How to understand the difference between ref toRef and toRefs in Vue3
  • Do you know ref, computed, reactive and toRefs of Vue3?

<<:  CSS uses Alibaba vector library to quickly add good-looking icon effects to the corresponding positions (example code)

>>:  MySQL 8.0.25 installation and configuration method graphic tutorial

Recommend

MYSQL transaction tutorial Yii2.0 merchant withdrawal function

Preface I am a PHP programmer who started out as ...

How to implement remote connection for Redis under Linux

After installing Redis on Linux, use Java to conn...

How to implement Linux deepin to delete redundant kernels

The previous article wrote about how to manually ...

Web Design Experience: Self-righteous Web Designers

1. Trash or Classic? Web technology updates very ...

Implementation of Element-ui Layout (Row and Col components)

Table of contents Basic instructions and usage An...

How to decompress multiple files using the unzip command in Linux

Solution to the problem that there is no unzip co...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

Setting up shadowsocks+polipo global proxy in Linux environment

1. Install shadowsocks sudo apt-get install pytho...

Detailed explanation of setting resource cache in nginx

I have always wanted to learn about caching. Afte...

Detailed example of MySQL data storage process parameters

There are three types of MySQL stored procedure p...