Home>Article>Web Front-end> Get to know Vue’s new generation state management library--Pinia

Get to know Vue’s new generation state management library--Pinia

青灯夜游
青灯夜游 forward
2022-08-30 19:56:30 1366browse

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!

Get to know Vue’s new generation state management library--Pinia

What is Pinia

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.

How to use Pinia

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)

Understanding Store

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, includingstate, getters, actions

1. Define a store

  • Store is defined using defineStore(),

  • and it requires a unique name, as The first parameter is passed

Get to know Vue’s new generation state management library--Pinia

2. Use store

Get to know Vue’s new generation state management library--Pinia

Get to know Vue’s new generation state management library--Pinia

Operation State

state is the core part of the store, and the store is used to implement our state management.

Get to know Vue’s new generation state management library--Pinia

  • Method 1: Modify state one by one directly
  • Method 2: Modify multiple states at once
  • Method 3: Replace state
  • Method 4: Reset state

Get to know Vue’s new generation state management library--Pinia

Get to know Vue’s new generation state management library--Pinia

Get to know Vue’s new generation state management library--Pinia

Get to know Vue’s new generation state management library--Pinia

##Getters

1 . Understand and define Getters

Gettersis equivalent to the calculated properties of Store:

    can be defined using the getters attribute in defineStore();
  • Getters can define a function that accepts a state as a parameter;

2. Access Getters

  • 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 

Get to know Vue’s new generation state management library--Pinia

Understand 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.

Get to know Vue’s new generation state management library--Pinia

Action supports asynchronous operations, so you can use await.

Get to know Vue’s new generation state management library--Pinia

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete