Introduction to reactive function toRef function ref function in Vue3

Introduction to reactive function toRef function ref function in Vue3

Reactive Function

reactive is used to define responsive data (which can be understood as a substitute for data)

usage:

import { reactive } from 'vue'

use:

const state = reactive({
    parameter name: parameter value})

Access: state.parameter name

Access: state.parameter name

toRef function (just understand it)

toRef: Extract a field from the responsive data into a separate responsive data

usage:

import import { toRef } from 'vue'

use:

const state = reactive({
    num:0
})
const num=toRef(state(responsive data),'num attribute name')
num:{
    value:0
}
Ref can actually be understood as a data type: {value: value}

Access: num.value===0

Note:

HTML: You don't need to write value when using responsive data

Value must be written in js

ref function

Defining Responsive Data

{
    value:value}

Direct definition using

import import {ref} from 'vue'
setup(){
Define const num = ref({a:1,b:2})  
    num:{
       value:{a:1,b:2}
    }
}

Access: num.value===0

reactive: Applicable to multiple data, ref is applicable to single data

Get DOM

<template>
  <div ref="target">123</div>
</template>
scripte
import {ref} from 'vue'
setup(){
   const target = ref(null)   
   return {target} 
   target.value is the corresponding DOM   
}

Get component instance object

ref is used for native tags to get dom

ref is used for component tags to obtain component instance objects

The usage is the same as getting DOM

<template>
  <component tag ref="target">123</component tag>
</template>
script
import {ref} from 'vue'
setup(){
   const target = ref(null)   
   return {target} 
   target.value is the component instance object}

The above is the detailed content of the introduction of the reactive function toRef function ref function in Vue3. For more information about Vue3 functions, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Reactive function declaration array method in Vue3
  • In-depth understanding of reactive in Vue3
  • Do you know ref, computed, reactive and toRefs of Vue3?
  • setup+ref+reactive implements vue3 responsiveness
  • Detailed analysis of the difference between Ref and Reactive in Vue3.0
  • Solution to reactive not being able to assign values ​​directly in Vue3

<<:  Differentiate between null value and empty character ('') in MySQL

>>:  Linux directory switching implementation code example

Recommend

Play with the connect function with timeout in Linux

In the previous article, we played with timeouts ...

Detailed examples of Zabbix remote command execution

Table of contents one. environment two. Precautio...

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

The most convenient way to build a Zookeeper server in history (recommended)

What is ZooKeeper ZooKeeper is a top-level projec...

Detailed explanation of Bind mounts for Docker data storage

Before reading this article, I hope you have a pr...

Explanation of the configuration and use of MySQL storage engine InnoDB

MyISAM and InnoDB are the most common storage eng...

How to create a MySQL database and support Chinese characters

Let's first look at the MySQL official docume...

MySQL 8.0.14 installation and configuration method graphic tutorial (general)

MySQL service 8.0.14 installation (general), for ...

SystemC environment configuration method under Linux system

The following is the configuration method under c...

Native js realizes the drag and drop of the nine-square grid

Use native JS to write a nine-square grid to achi...

Some tips on website design

In fact, we have been hearing a lot about web des...

Tutorial on installing MySQL 5.6 on CentOS 6.5

1. Download the RPM package corresponding to Linu...

Vue3 Documentation Quick Start

Table of contents 1. Setup 1. The first parameter...

HTML+jQuery to implement a simple login page

Table of contents Introduction Public code (backe...