In Vue applications, inter-component communication is a common requirement when building complex applications. Although parent-child components can be passedprops
and$emit
This method seems unscrupulous when sharing states across multiple levels of components or sibling components. Providedprovide
andinject
API makes cross-component communication more concise and efficient. This article will explore in-depth how to use itprovide
andinject
Implement cross-component communication in it and explain it step by step through sample code.
Scene setting
Suppose we are developing a task management application that contains a task list component (TaskList
) and multiple task item components (TaskItem
). and want to be in the top-level component of the application (App
) defines a state (such as the currently selected task) in , and letTaskItem
Components can access and modify this state without having to pass layer by layer through each layer of components in the middle.
Step 1: Provide status in the top-level component
First, inApp
In the component, useprovide
Functions provide a state and a method so that descendants can access and modify this state.
// <script setup> import { reactive, provide } from 'vue'; import TaskList from './components/'; const appState = reactive({ selectedTask: null }); const selectTask = (task) => { = task; }; // Use provide to provide state and methodprovide('appState', appState); provide('selectTask', selectTask); </script> <template> <TaskList /> </template>
Used in the above codereactive
Created a responsive stateappState
, and defines a methodselectTask
Used to update this state. Then, throughprovide
Functions provide these to all descendants.
Step 2: Inject state in the child component
existTaskItem
In the component, useinject
Functions are injected inApp
The state and methods provided in the component.
// <script setup> import { inject } from 'vue'; // Inject state and methodconst appState = inject('appState'); const selectTask = inject('selectTask'); const handleSelect = () => { selectTask('Current task'); }; </script> <template> <div @click="handleSelect">Select a task:{{ }}</div> </template>
In this example,TaskItem
Components can be accessed and modified directly byApp
Components providedappState
andselectTask
, without passingprops
Pass layer by layer.
Key points analysis
Responsive state sharing:
provide
andinject
With a responsive system, state sharing becomes very flexible in Vue applications. quiltprovide
The provided responsive states remain responsive in the descendant components, and any state changes are reflected in the components that depend on these states in real time.Type safety:Although
inject
A default value can be accepted, but for better type safety and code readability, it is recommended to use the same identifier when providing and injecting or use TypeScript type annotations.Use scenarios:
provide
andinject
Especially suitable for developing cross-component level functions, such as theme switching, user preferences, global state management, etc. They provide more flexible architectural design possibilities for Vue applications.
Conclusion
By usingprovide
andinject
, The inter-component communication of applications becomes more flexible and efficient, especially when dealing with state sharing across component levels. This pattern not only reduces code redundancy, but also makes the component structure clearer, helping to build more maintainable and scalable Vue applications.
This is the article about using provide/inject to implement cross-component communication. For more related cross-component communication content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!