Global State Management with Vue's Provide/Inject
Vue 3 introduced provide and inject as a mechanism for dependency injection, offering a way to share data across components without explicitly passing props down through multiple levels of the component tree. This challenge focuses on implementing a simple app-level state management solution using provide and inject to manage a user's authentication status. This is a common pattern for sharing authentication information throughout your application.
Problem Description
You need to create a Vue application that manages a user's authentication status (logged in or logged out) using provide and inject. The application should have a central component (App.vue) that provides the authentication state. Other components can then inject this state and react accordingly. The state should be mutable, allowing components to log the user in or out.
Key Requirements:
- App.vue (Provider): This component will be the root of your application and will
provideanauthStoreobject.authStorewill have two properties:isLoggedIn(boolean, initiallyfalse) and aloginfunction, and alogoutfunction. - authStore: This object should be provided using
provide('auth', authStore). - Injectable Store: Other components should be able to
injecttheauthStoreusinginject('auth'). - Login/Logout Functionality: The
loginfunction should setisLoggedIntotrue. Thelogoutfunction should setisLoggedIntofalse. - Component Reactivity: Components that inject the
authStoreshould re-render whenisLoggedInchanges.
Expected Behavior:
- Initially, all components that inject
authStoreshould display a message indicating the user is logged out. - When the
loginfunction is called (e.g., from a button click),isLoggedInshould be set totrue, and all components that injectauthStoreshould update to display a message indicating the user is logged in. - When the
logoutfunction is called,isLoggedInshould be set tofalse, and all components that injectauthStoreshould update to display the logged-out message.
Edge Cases to Consider:
- Ensure that the
authStoreis properly initialized and provided. - Handle potential errors if the
injectfails (though this is unlikely in a controlled environment). - Consider how to manage the state if the application needs to persist the authentication status across page reloads (this is beyond the scope of this challenge, but a good consideration for real-world applications).
Examples
Example 1:
Input: App.vue provides authStore with isLoggedIn = false, and a component injects authStore.
Output: The injected component displays "Logged Out".
Explanation: The initial state is logged out, so the component reflects that.
Example 2:
Input: A button click calls authStore.login().
Output: The injected component displays "Logged In".
Explanation: The login function sets isLoggedIn to true, triggering a re-render.
Example 3:
Input: A button click calls authStore.logout().
Output: The injected component displays "Logged Out".
Explanation: The logout function sets isLoggedIn to false, triggering a re-render.
Constraints
- The solution must be implemented using Vue 3 and TypeScript.
- The solution must use
provideandinjectfor app-level state management. - The solution should be well-structured and easy to understand.
- The solution should be functional and meet all the requirements outlined in the problem description.
- No external state management libraries (e.g., Vuex, Pinia) are allowed. The goal is to demonstrate understanding of
provide/inject.
Notes
- Think about how to structure your components to clearly separate the state management logic from the UI.
- Consider using a reactive object for
authStoreto ensure that changes are properly detected by injected components. - This is a simplified example. In a real-world application, you would likely use a more robust state management solution. However, this exercise is designed to illustrate the basic principles of
provideandinject. - Focus on the core functionality of providing and injecting the authentication state. Styling and complex UI elements are not required.