Home>Article>Web Front-end> Get to know Vue’s new generation state management library--Pinia
What is pinia? how to use? This article will take you to learn aboutVue's new generation of state management library - Pinia. I hope it will be helpful to you!
Pinia (pineapple in Spanish) is essentially still astate management Library, used for state sharing across components and pages. [Related recommendations:vue.js video tutorial]
The difference between pinia and vuex:
More friendly TypeScript support, Vuex previously supported TS The support is very unfriendly
Compared with Vuex, Pinia provides a simpler API with fewer rituals and provides a Composition-API style API
There is no more nested structure of modules
There is no longer the concept of namespace, and there is no need to remember their complex relationships.
1. Install pinia
yarn add pinia
2. Create a pinia
// src/stores/index.js import { createPinia } from "pinia"; const pinia = createPinia() export default pinia
//main.js import pinia from './stores' app.use(pinia)
A Store (such as Pinia) is an entity that holds thestate and business logicthat are bound to your component tree, that is, saved With the global state
, you can define any number of Stores to manage your state, including
state, getters, actions
Store is defined using defineStore(),
and it requires a unique name, as The first parameter is passed
state is the core part of the store, and the store is used to implement our state management.
Gettersis equivalent to the calculated properties of Store:
Method 1: Access the Getters of the current store
Method 2: Access other Getters of your own in Getters
Method 3: Access Getters of other stores
getters: { // 1. 基本使用 debouleCount(state) { return state.count * 2 }, // 2. 一个 getters 引入另外一个 getters useDebouleCount() { return this.debouleCount + 2 }, // 3. getter也支持返回一个函数 getFriendById(state) { return function (id) { for (let i = 0; iUnderstand and define Action
Action can be understood as methods in the component. Like getters, all operations of the entire store instance can be accessed through this in the action.Action supports asynchronous operations, so you can use await.For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of Get to know Vue’s new generation state management library--Pinia. For more information, please follow other related articles on the PHP Chinese website!