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
vmware vsphere 6.5 is the classic version of vsph...
Less is More is a catchphrase for many designers....
First, before posting! Thanks again to I Want to S...
The overall architecture of MySQL is divided into...
This article mainly introduces three methods of i...
Table of contents Preface text 1. Install styleli...
As shown in the figure: Table Data For such a tre...
This article describes the Linux user and group c...
The database installation tutorial of MySQL-8.0.2...
Demand scenario: The boss asked me to use the cra...
Introduction After compiling, installing and solv...
SQL Left Join, Right Join, Inner Join, and Natura...
This article example shares the specific code of ...
1. Fixed width + adaptive Expected effect: fixed ...
Copy code The code is as follows: <!DOCTYPE HT...