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
Table of contents Introduction effect principle f...
This article example shares the specific code of ...
Concurrency Functions time for i in `grep server ...
Uninstall MariaDB CentOS7 installs MariaDB instea...
If you want to hide content from users of phones, ...
Jenkins configuration of user role permissions re...
Table of contents Preface Parsing parameters Modi...
Make a blank space for Taobao: When you shrink th...
Note: This article is about the basic knowledge p...
The fixed layout of the page header was previousl...
Table of contents Preface Cause analysis and solu...
Now if you want to use the video tag in a page, y...
1. Business Scenario I have been doing developmen...
I am writing a small program recently. Because th...
In the previous article, we explained how nginx r...