1. What is Pinia? So there was the emergence of Pina Compared with vuex:
The Pinia documentation has this to say:
So learning 2. Pinia is easy to useFirst, we initialize a vite+vue+ts project pnpm create vite pinia-demo -- --template vue-ts Install pinia pnpm i pinia Open the project, edit the main.ts file in the src directory, and import Pinia //main.ts import { createApp } from 'vue' import App from './App.vue' import { createPinia } from 'pinia' createApp(App).use(createPinia()).mount('#app') Create a import { defineStore } from 'pinia' export const useCounterStore = defineStore('counter', { state: () => { return { count: 0, } }, getters: { doubleCount: (state) => state.count * 2, }, actions: { increment() { this.count++ }, }, })
We can also use a callback function to return export const useCounterStore = defineStore('counter', () => { const count = ref(0) function doubleCount() { return count.value * 2 } function increment() { count.value++ } return { count, increment } }) Next we use <script setup lang="ts"> import { useCounterStore } from './store/counter' const useCounter = useCounterStore() </script> <template> <h2>{{ useCounter }}</h2> <h2>{{ useCounter.count }}</h2> <h2>{{ useCounter.doubleCount() }}</h2> <button @click="useCounter.increment">increment</button> </template> <style> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style> In the process of using The browser runs as follows: Open the developer tools to view Pinia has multiple ways to modify the state:
const countPlus_1 = useCounter.count++ Use const countPlus_2 = useCounter.$patch({ count: useCounter.count + 1 }) const countPlus_3 = useCounter.$patch((state) => state.count++) The const { count } = storeToRefs(useCounter) 3. User experience For small projects, state management focuses more on convenience and speed (you may not even need it), so vuex is a little complicated. When vue3 was released in This is the end of this article about Vue state management using Pinia instead of Vuex. For more relevant content about using Pinia instead of Vuex, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Common functions of MySQL basics
>>: Web design dimensions and rules for advertising design on web pages
The same server simulates the master-slave synchr...
This article example shares the specific code of ...
Today I found that WordPress could not connect to...
Table of contents Introduction to Arrays Array li...
Table of contents plan Install Dependencies Intro...
One trick for dealing with this type of error is t...
Table of contents 1. Pull the mysql image 2. Chec...
This blog post is about a difficulty encountered ...
1. There are two ways to modify global variables ...
Table of contents Cause of the incident Use Node ...
Generally, learning Java and deploying projects a...
1. Set CORS response header to achieve cross-doma...
Everyone is familiar with the meta tag in desktop...
The method of obtaining the position of the point...
Chatbots can save a lot of manual work and can be...