What is pinia? how to use? This article will take you to learn about Vue's new generation of state management library - Pinia. I hope it will be helpful to you!
Pinia (pineapple in Spanish) is essentially still a state 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 the state and business logic that 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.
Getters is 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; i <p><img src="https://img.php.cn/upload/article/000/000/024/9b758e20089c830456d8b17003d54ea1-8.png" alt="Get to know Vues new generation state management library--Pinia"></p><h2>Understand and define Action<strong></strong> </h2><blockquote>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. <p></p> </blockquote><p><img src="https://img.php.cn/upload/article/000/000/024/4cc76dfa4ba802a30963e696c1128d1f-9.png" alt="Get to know Vues new generation state management library--Pinia"></p><blockquote>Action supports asynchronous operations, so you can use await. <p></p> </blockquote><p><img src="https://img.php.cn/upload/article/000/000/024/4cc76dfa4ba802a30963e696c1128d1f-10.png" alt="Get to know Vues new generation state management library--Pinia"></p>For more programming-related knowledge, please visit: <p>Introduction to Programming<a href="https://www.php.cn/course.html" target="_blank" textvalue="编程入门">! ! </a></p>
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!