In Vue development, we usually use single-file components (.vue files) for development and split the components to improve the readability and maintainability of the code. But when calling methods between multiple single-file components, some problems may arise. This article will introduce several ways to call methods between multiple files in Vue.
1. Use Mixin (Mixin)
Mixin is a very convenient method provided by Vue, which can encapsulate some common code and share it between multiple components. We can write the methods that need to be shared in a Mixin and introduce this Mixin into the components that need to use these methods.
For example, we have a Mixin file mixin.js, which contains a showMsg method:
export default { methods: { showMsg() { console.log('hello world') } } }
We only need to introduce mixin.js into the component that uses the showMsg method.
import mixin from './mixin.js' export default { mixins: [mixin], ... }
In the above code, we use the mixins attribute to introduce the code in mixin.js and add it to the component.
In this way, you can use the showMsg method in the component.
this.showMsg()
2. Use this.$refs
When we define a component in Vue, we can set a ref attribute to it, and we can reference this component instance in the component. This ref can be used to call methods of the current component.
For example, we have two components, component A and component B. Component A calls the method in component B. This can be achieved through the following steps:
- Set the ref attribute in component B
<template> <div> 组件B </div> </template> <script> export default { methods: { methodOfB() { console.log('methodOfB') } } } </script>
<template> <div> <component-b></component-b> </div> </template> <script> import ComponentB from './ComponentB.vue' export default { components: {ComponentB}, methods: { methodOfA(){ this.$refs.b.methodOfB() } } } </script>
Use this.$refs.b in the method in component A to get the instance of component B, thereby using the method of component B.
3. Use vuex
Vuex is a state management tool officially provided by Vue. It is very convenient to share data between multiple components. In addition, we can also define some public methods in Vuex for use by multiple components.
For example, we have a store.js file in which a public method is defined:
export default new Vuex.Store({ state: {...}, mutations: {...}, actions: {...}, getters: {...}, methods: { showMsg() { console.log('hello world') } } })
In the component that needs to use this public method, we can use the mapActions method provided by Vuex to This method is mapped to the component.
import { mapState, mapMutations, mapActions } from 'vuex' computed: { ...mapState(['...']), }, mutations: { ...mapMutations(['...']), }, actions: { ...mapActions(['showMsg']), }
This way you can use the showMsg method in the component.
4. Using EventBus
We can use Vue’s own EventBus to communicate between components, bind the methods that need to be shared to EventBus, and other components can use these methods. .
For example, we create an EventBus in main.js:
import Vue from 'vue' export const EventBus = new Vue()
In components that need to use shared methods, we can use the EventBus.$on method to listen to an event to get the shared method to be called news.
mounted(){ EventBus.$on('showMsg', ()=>{this.showMsg()}) },
In components with shared methods, we can use the EventBus.$emit method to trigger the showMsg event.
EventBus.$emit('showMsg')
Summary
The above are several ways to call methods between multiple files Vue. Through Mixin, this.$refs, Vuex and EventBus, we can easily realize the need to share methods in multiple components. In actual development, we can choose a method that suits us according to the actual situation.
The above is the detailed content of Several ways to call methods between multiple files Vue. For more information, please follow other related articles on the PHP Chinese website!

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
