Vuex and Pinia are state management libraries that provide a centralized, predictable state management solution for Vue applications.
Vuex is one of the officially recommended status management libraries. Its core concepts include state, mutation, action, and getter. Where state represents the application's state data and is stored as a unique source in Vuex. Mutation is used to modify the state data and ensure the traceability of data changes. Action is used to handle asynchronous operations or combine multiple mutation operations. Getter allows us to calculate and derive state and make it easier to access. A Vuex store instance is a global JavaScript object that can be accessed and operated through injection in all components.
Install and introduce Pinia
Install:npm install pinia
oryarn add pinia
Introduce registration:
How to introduce Vue3main.
<template> <div>pinia-current:{{ }}</div> <div>pinia-name:{{ }}</div> </template> <script setup lang="ts"> import { useTestStore } from "./store"; const Test = useTestStore(); </script> <style lang="scss" scoped></style>
Bulk modification of factory function form
Recommended function form You can customize the logic
<template> <div>pinia-current:{{ }}</div> <div>pinia-name:{{ }}</div> <div style="display: flex; flex-direction: column"> <button @click="funChange">Factory function implementation batchchange</button> </template> <script setup lang="ts"> import { useTestStore } from "./store"; const Test = useTestStore(); const funChange = () => { Test.$patch((state) => { ( = 999), ( = "Little 3"); }); }; </script> <style lang="scss" scoped></style>
This is the end of this article about the implementation example of vue's state library management. For more related vue's state library management content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!