Vue methods call each other's methods

 

methods is a built-in function, mainly used for data transfer between two components, that is, calling methods. Let me introduce a method to call each other in vue, you can refer to it during use. Methods realize the transfer of data between two components. Let’s take a look at how Methods realize data transfer: Methods:: Object () -> method name Step 1: We first call the Object () function Step 2: Use method -> method name Step 3: The return value can realize the data transfer between components through methods, but if you want to realize the data transfer between two components, you may need to call another function to achieve it.

  • 1. How to use

    In our project, some data needs to be passed between component A and component B, which can be achieved through a function. In vue, we first need to create a new component, and then use function definitions to achieve data transfer between the two components. Next, use this function to call the object () method in vue to realize the data transfer between the two components, but since both components are an object in the Vue project, there will be two problems: First, when calling the method Before that we need to initialize the function. In fact, in this function, you can also call the method you defined before. So why not just use a function to do it? The main reason is that there is currently no native method in vue to transfer data between two components. So we can only achieve it through the method provided by the third party. There are mainly two methods provided by third parties: Using the methods provided by third parties requires the use of third-party libraries or third-party services. Third-party libraries or third-party services are generally provided to us through API interface calls. In this way we can achieve data transfer between the two components. According to the above two methods, we can know that we only need to call the method defined before ourselves in the function. This is because in vue, we don't need to call our previously defined function when we define a function, but if we need to use a certain method but don't know how to call this method, we need to find out what the method is where it is used. And vue provides such an API interface for us to use. For data transfer through methods, first call your previously defined method (such as Object ()) in the method, and then use this method to call your previously defined method (such as Object ())). The above is the process of how to use methods for data transfer in vue. In the above example, we implemented data transfer between component A and component B through function calls, but in order to better understand this function and how it implements data transfer, we also need to know what else can be done in vue Use methods to transfer data.

  • Two, the return value

    In order to better understand this function, we can take a look at what its return value is: When we assign the parameters in the function to the Object () function, it will return an Object () object, which contains The return value of the Object() function. When we use the method, we will call the Object () function to get the data of the component. After getting the data of the component, we will return it to vue by returning the value, just like the one introduced above. In addition to the methods introduced above, there is another method to transfer data between components, which will be demonstrated below. The return value of a function is usually an object or an array. But sometimes we use composition to transfer data between components. There are two methods in vue: Object and Methods. So when using vue to transfer data between two components, you can use Methods to achieve it, that is, the Object () function. In the above example, we just called the Object() function to get data between components. If we need to get data between components, we can call the function again: So how do we combine two components? Let's take a look: Two methods are included in the two Objects: one is the Object () method, and the other is the methods () method. The above two Objects use two methods in Object to transfer data between components: Through the above two examples, we can clearly see how to transfer data between components in Vue.

  • 3. Summary

    methods is a built-in method that can pass the return value of the function to the component, which can be said to be a very convenient function. However, since the return value of a function is a method, the return value needs to be assigned to the method when the function is called, so if some complex data needs to be passed, it may involve a method call, which will be troublesome. A relatively simple example is given below, and we can realize the transfer between methods according to this example: First, we need to define a function, which is methods. Then we need two components: 1. One component contains three methods (mutation) By calling the methods:: Object () function, we can realize the data transfer between the three components. These three methods are the methods that methods call each other. If you want to implement more complex functions, you may need to call more methods. Of course, you can also call a function to transfer data between two components, which will be simpler. The above is a vue interoperability method shared with you, I hope it will be helpful to you!

Commonly used vue methods call each other code:

In Vue, we can define some methods in methods, and then call these methods in other methods. Here are some examples:
1. Calculate the sum of two numbers:
```javascript
new Vue({ el: '#app', data: { a: 5, b: 10 }, methods: { sum: function() { return this.a + this.b; }, showSum: function() { alert('The sum of the two numbers is:' + this.sum()); } } }); ``` 2. Change the text color: ` ``javascript new Vue({ el: '#app', data: { textColor: 'red' }, methods: { changeColor: function(color) { this.textColor = color; }, changeToRed: function() {



























this.changeColor('red');
},
changeToBlue: function() {
this.changeColor('blue');
}
}
});
```
3. 切换显示/隐藏元素:
```javascript
new Vue({
el: '#app',
data: {
isVisible: true
},
methods: {
toggleVisibility: function() {
this.isVisible = !this.isVisible;
},
showElement: function() {
if (!this.isVisible) {
this.toggleVisibility();
}
},
hideElement: function() {
if (this.isVisible) {
this.toggleVisibility();
}
}
}
});
```
In these examples, we define some methods and call them in other methods. This makes the code more modular and reusable.

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130969637