How to use variable promotion in the setup() function in Vue3's combined API?

The combined API of Vue3 is really eye-catching! This is a very basic concept for variable promotion in the setup() function.

First of all, we need to understand that the setup() function is a brand new thing in Vue3. It's a bit like our superheroes need to do warm-up exercises before they become superheroes. The setup() function is that warm-up exercise. It allows us to define some variables and functions in advance so that we can use them at any time when we really need them.

So, how to use variable promotion in the setup() function? It's actually very simple, just like in our daily lives, if we want to lift a heavy object, we first need to use some strength to lift it, right? In Vue3, the setup() function is like lifting the heavy object in advance. We first define some variables in the setup() function so that we can use them at any time in our components.

Looking at a simple example, we can define a variable called "count" in the setup() function and then use it in the component:

import {
    
     ref } from 'vue';  
  
export default {
    
      
  setup() {
    
      
    const count = ref(0);  
      
    function increment() {
    
      
      count.value++;  
    }  
      
    return {
    
      
      count,  
      increment  
    };  
  }  
};

In this example, we define a variable named "count" in the setup() function and wrap it into a reactive reference using the ref() function. Then, we define a function called "increment", which can increase the value of "count" by 1. Finally, we return both "count" and "increment" so that they can be used anytime in our component.

When we need to use "count" and "increment", we only need to call them in the template, like this:

<template>  
  <div>  
    <p>Count: {
   
   { count }}</p>  
    <button @click="increment">Increment</button>  
  </div>  
</template>

In this template, we display the value of "count" and when the user clicks the button, the "increment" function is called to increase the value of "count" by 1.

In short, the setup() function in Vue3's combined API allows us to manage our component state more conveniently, and variable promotion allows us to more easily use the variables and functions defined in the setup() function. It's like we lift the dumbbell in advance, and then we can use it directly during fitness without having to work hard to lift it.

I hope this metaphor can help you understand the concept of setup() function and variable promotion in Vue3's combined API!

When we use the composition API in Vue3, we will use more functions and tools to build our components. One very important function is reactive(), which allows us to convert a normal JavaScript object into a reactive object.

Imagine we have a component that receives an object called userData as props. This object contains some user information, such as name, age, etc. We want to display this information in the component, and when the userData object changes, the component's display should update accordingly.

At this time, we can use the reactive() function to convert the userData object into a responsive object. In this way, when the userData object changes, Vue3 will automatically update the relevant components, and we do not need to manually call the render() function or $forceUpdate() method.

Here is an example using the reactive() function:

import {
    
     reactive } from 'vue';  
  
export default {
    
      
  setup() {
    
      
    const userData = reactive({
    
      
      name: 'John Doe',  
      age: 30,  
      gender: 'male'  
    });  
      
    function updateUserData(newData) {
    
      
      userData.value = newData;  
    }  
      
    return {
    
      
      userData,  
      updateUserData  
    };  
  }  
};

In this example, we use the reactive() function to create a reactive object named userData. This object contains a name, an age, and a gender. Then, we define a function called updateUserData, which updates the value of the userData object to the new data.

When we need to update the value of the userData object, we only need to call the updateUserData() function and pass in the new data object as a parameter. This function will assign the new data object to userData.value, thereby updating the value of the userData object.

In short, the reactive() function in Vue3's combined API allows us to create and manage reactive objects more conveniently. By using the reactive() function, we can more easily implement automatic updates of components and better manage the state of our components.

Guess you like

Origin blog.csdn.net/2301_77795034/article/details/131457683