Home Web Front-end JS Tutorial How vue calls methods externally

How vue calls methods externally

Apr 12, 2018 pm 05:23 PM
methods external transfer

This time I will show you how vue calls methods externally. What are the precautions for vue to call methods externally. Here is a practical case, let’s take a look.

1. First define a public vue component;

var eventHub = new Vue();
Copy after login

2. In the event of the current component, in created, use $on to pass to the public component eventHub, translate is customized, and getCardNum(data) is the method to be called externally;

eventHub.$on('translate', function (data) { 
        that.getCardNum(data); 
      });
Copy after login

3. Finally, in the parent component, note that the negative component should be saved in a variable, var vm = new Vue({});

4. Define a method in the methods method in the parent component, and use $emit in the method to receive the method in the public component;

var vm = new Vue({ 
 el: '#example', 
 data: { 
  msg: 'Hello Directive', 
  data: {} 
 }, 
 methods: { 
  getCardNum: function (data, on) { 
   eventHub.$emit('translate', data); 
  } 
 } 
});
Copy after login

5. Finally, you can call the getCardNum(data) function outside the vue component or outside the file. For example, in HTML, you can call it like onclick = vm.getCardNum(); vm is the parent component

6. Be sure to write the variable name of the parent component as vm.getCardNum();

When I was developing with vue, I encountered a pop-up page in the java background that wanted to call a method in my vue component. However, the pop-up page in the background was not in my vue component, and other pages wanted to call it in vue. The method can only be called in the parent component, so I studied it for a long time and finally decided to pass the function() method in the component to the top-level parent component, save the negative component in a variable, and finally directly add it to other pages When calling the method, you cannot use the @click method to call it, because the background page is not inside my vue component, so the call is onclick = vm.getCardNum(); When called like this, vm is the parent component

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to get the selection value when using the mint-ui time plug-in

How to operate data binding in vue checkbox Determination and acquisition and calculation

The above is the detailed content of How vue calls methods externally. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

This workbook contains links to one or more external sources that may be unsafe This workbook contains links to one or more external sources that may be unsafe Feb 26, 2024 am 11:07 AM

Does Excel continue to throw This workbook contains links to one or more external sources that may display an unsafe warning message when opening the workbook? Many users have reported that they receive this warning whenever they open an Excel file. Although this warning indicates that there are potentially malicious links in the workbook, it may be triggered even if you have included a trusted external source. This workbook contains links to one or more potentially unsafe external sources if you open Excel. The warning "This workbook contains links to one or more external sources that may be unsafe" appears when you open a file. You can try the following solutions to resolve the issue: Check external links in the workbook and remove untrusted links. . Use the edit link feature

How to use Python to call Baidu Map API to implement geographical location query function? How to use Python to call Baidu Map API to implement geographical location query function? Jul 31, 2023 pm 03:01 PM

How to use Python to call Baidu Map API to implement geographical location query function? With the development of the Internet, the acquisition and utilization of geographical location information is becoming more and more important. Baidu Maps is a very common and practical map application that provides a wealth of geographical location query services. This article will introduce how to use Python to call Baidu Map API to implement the geographical location query function, and attach a code example. Apply for a Baidu Map developer account and application First, you need to have a Baidu Map developer account and create an application. Log in

Source code exploration: How are objects called in Python? Source code exploration: How are objects called in Python? May 11, 2023 am 11:46 AM

Wedge We know that objects are created in two main ways, one is through Python/CAPI, and the other is by calling a type object. For instance objects of built-in types, both methods are supported. For example, lists can be created through [] or list(). The former is Python/CAPI and the latter is a calling type object. But for instance objects of custom classes, we can only create them by calling type objects. If an object can be called, then the object is callable, otherwise it is not callable. Determining whether an object is callable depends on whether a method is defined in its corresponding type object. like

PHP camera calling skills: How to implement multi-camera switching PHP camera calling skills: How to implement multi-camera switching Aug 04, 2023 pm 07:07 PM

PHP camera calling skills: How to switch between multiple cameras. Camera applications have become an important part of many web applications, such as video conferencing, real-time monitoring, etc. In PHP, we can use various technologies to call and operate the camera. This article will focus on how to implement multi-camera switching and provide some sample code to help readers better understand. Basics of camera calling In PHP, we can call the camera by calling the JavaScript API. Specifically, we

How to solve external resource access and calls in PHP development How to solve external resource access and calls in PHP development Oct 08, 2023 am 11:01 AM

How to solve the problem of accessing and calling external resources in PHP development requires specific code examples. In PHP development, we often encounter situations where we need to access and call external resources, such as API interfaces, third-party libraries or other server resources. When dealing with these external resources, we need to consider how to access and call safely while ensuring performance and reliability. This article describes several common solutions and provides corresponding code examples. 1. Use the curl library to call external resources. Curl is a very powerful open source library.

How to call Baidu Map API through Python programming to implement map display function? How to call Baidu Map API through Python programming to implement map display function? Aug 02, 2023 pm 08:27 PM

How to call Baidu Map API through Python programming to implement map display function? With the rapid development of the Internet, map applications have become an indispensable part of our lives. As one of the largest map applications in China, Baidu Maps provides us with a wealth of services and API interfaces, which can easily realize the map display function. This article will introduce how to call Baidu Map API through Python programming to realize the map display function, and give corresponding code examples. First, we need to register a

How matlab calls m files-matlab method of calling m files How matlab calls m files-matlab method of calling m files Mar 04, 2024 pm 01:49 PM

Many friends still don’t know how to call m files in matlab, so the editor below explains how to call m files in matlab. If you are in need, please take a look. I believe it will be helpful to everyone. 1. First open the matlab software and click "Open" in the main interface, as shown in the figure below. 2. Then select an m file that needs to be opened and select Open, as shown in the figure below. 3. Then look at the file name and number of variables of the m file in the editor, as shown in the figure below. 4. You can enter the m file name followed by the variable value in brackets on the command line to call it, as shown in the figure below. 5. Finally, the m file can be successfully called, as shown in the figure below. The above is the complete description of how to call m files in matlab brought to you by the editor.

Vue error: The function in methods cannot be used correctly, how to solve it? Vue error: The function in methods cannot be used correctly, how to solve it? Aug 18, 2023 pm 06:30 PM

Vue error: The function in methods cannot be used correctly, how to solve it? In the process of using Vue.js for front-end development, we often encounter the problem of being unable to use functions in methods correctly. This situation often occurs when we define a function but cannot call the function in the Vue component. This article will introduce some common error causes and solutions, and illustrate them with code examples. Error reason 1: The function is not correctly bound to the Vue instance when we use methods

See all articles