Vue.js $refs usage case explanation

Vue.js $refs usage case explanation

Despite props and events, sometimes you still need to access child components directly in JavaScript. To do this, you can use ref to assign a reference ID to the child component.

ref specifies a reference ID for the child component, so that the parent component can directly access the data in the child component through ref

This.$refs.outsideComponentRef can directly locate ref="outsideComponentRef" and return the instantiated object

1. Ref is used on external components

<div id="app">
    <component-father ref="outsideComponentRef"></component-father>
</div>

<script>
    var refoutsidecomponentTem = {
        template: "<div class='childComp'><h5>{{test}}</h5></div>",
        data(){
            return {
                test:'I am a child component'
            }
        }
    };

    new Vue({
        el: "#app",
        components:
            "component-father": refoutsidecomponentTem
        },
        mounted:function () {
            console.log(this); // #app vue instance console.log(this.$refs.outsideComponentRef); // VueComponent vue instance console.log(this.$refs.outsideComponentRef.test); // 'I am a child component'
        }
    });
</script>

2. Ref is used on external elements

<div id="app">
    <component-father></component-father>
    <p ref="outsideComponentRef">p tag</p>
</div>

<script>
    var refoutsidecomponentTem = {
        template: "<div class='childComp'><h5>{{test}}</h5></div>",
        data(){
            return {
                test:'I am a child component'
            }
        }
    };

    new Vue({
        el: "#app",
        components:
            "component-father": refoutsidecomponentTem
        },
        mounted:function () {                
            console.log(this.$refs.outsideComponentRef); // Returns "<p>p标签</p>" object}
    });
</script>

This is the end of this article about the detailed case study of Vue.js $refs usage. For more relevant Vue.js $refs usage content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • setup+ref+reactive implements vue3 responsiveness
  • Detailed explanation of Vue's ref attribute
  • Refs and Ref Details in Vue3
  • Introduction to reactive function toRef function ref function in Vue3
  • Detailed analysis of the difference between Ref and Reactive in Vue3.0
  • Detailed explanation and extension of ref and reactive in Vue3
  • A brief analysis of the difference between ref and toRef in Vue3
  • The complete usage of setup, ref, and reactive in Vue3 combination API
  • Usage and demonstration of ref in Vue

<<:  Implementation of installing and uninstalling CUDA and CUDNN in Ubuntu

>>:  Detailed analysis of the principles and usage of MySQL views

Recommend

An article to help you learn more about JavaScript arrays

Table of contents 1. The role of array: 2. Defini...

VMware ESXi 5.5 deployment and configuration diagram process

Table of contents 1. Installation requirements 2....

Using js to implement the two-way binding function of data in Vue2.0

Object.defineProperty Understanding grammar: Obje...

CSS scroll bar style modification code

CSS scroll bar style modification code .scroll::-...

A brief analysis of HTML space code

How much do you know about HTML? If you are learni...

How to draw special graphics in CSS

1. Triangle Border settings Code: width: 300px; h...

A brief discussion on mobile terminal adaptation

Preface The writing of front-end code can never e...

The problem of Vue+tsx using slot is not replaced

Table of contents Preface Find the problem solve ...

Non-standard implementation code for MySQL UPDATE statement

Today I will introduce to you a difference betwee...

Detailed steps to build the TypeScript environment and deploy it to VSCode

Table of contents TypeScript environment construc...

A brief discussion on why daemon off is used when running nginx in docker

I'm very happy. When encountering this proble...

How to clean up the disk space occupied by Docker

Docker takes up a lot of space. Whenever we run c...

Vue3 manual encapsulation pop-up box component message method

This article shares the specific code of Vue3 man...

Analyze several common solutions to MySQL exceptions

Table of contents Preface 1. The database name or...