Home  >  Article  >  Web Front-end  >  How to use Vue3 pinia state management tool

How to use Vue3 pinia state management tool

WBOY
WBOYforward
2023-06-03 13:09:391078browse

What is pinia?

This is vue3's new state management tool. Simply put, it is equivalent to the previous vuex. It removes Mutations but also supports vue2. It is highly recommended. Because its logo looks like a pineapple, we also call it Big Pineapple.

This is vue3 a new state management tool. Simply put, it is equivalent to the previous vuex. It removes Mutations but also supports vue2 is highly recommended. Because its logo looks like a pineapple, we also call it Big Pineapple.

Installation command

npm i pinia

Use

1, mian.js introduced pinia, global registration

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
 
const app = createApp(App)
app.use(createPinia())
app.mount('#app')

2. Create a new index.js file in the store folder. If you support TS, you can create index.ts and introduce pinia into the file for storage. Status

import {defineStore} from 'pinia'
export const useUserStore = defineStore("USER",{
    state() {
        return {
            name: '景天',
            age: 18,
            name1: '胡歌',
            age1: 36
        }
    },
    // 和vuex一样
    getters: {
 
    },
    // 和vuex一样
    actions: {
        setAge() {
            this.age--
        }
    }
})

3. The page uses the status <pre class="brush:js;">&lt;template&gt; &lt;div&gt;正常取值&lt;/div&gt; &lt;div&gt;{{User.name}}&lt;/div&gt; &lt;div&gt;{{User.age}}&lt;/div&gt; &lt;div&gt;解构取值&lt;/div&gt; &lt;div&gt;{{name}}&lt;/div&gt; &lt;div&gt;{{age}}&lt;/div&gt; &lt;div&gt;解构取值转ref&lt;/div&gt; &lt;div&gt;{{name1}}&lt;/div&gt; &lt;div&gt;{{age1}}&lt;/div&gt; &lt;button @click=&quot;change1&quot;&gt;change1&lt;/button&gt; &lt;button @click=&quot;change2&quot;&gt;change2&lt;/button&gt; &lt;button @click=&quot;change3&quot;&gt;change3&lt;/button&gt; &lt;button @click=&quot;change4&quot;&gt;change4&lt;/button&gt; &lt;button @click=&quot;change5&quot;&gt;change5&lt;/button&gt; &lt;div&gt; &lt;button @click=&quot;handleReset&quot;&gt;重置&lt;/button&gt; &lt;/div&gt; &lt;/template&gt; &lt;script setup lang=&quot;ts&quot;&gt; import { storeToRefs } from &amp;#39;pinia&amp;#39;; import { useUserStore } from &amp;#39;./store&amp;#39;; // 获取store中的值 let User = useUserStore() // 通过ES6的结构取值,但是这个值不是响应式的 let {name,age} = User // 通过pinia自带的方法,转换成ref,就是响应式的了 let {name1,age1} = storeToRefs(User) // 改变store中值的方式有五种 // 方式一 function change1() { User.age++ } // 方式二,可一次性修改多个值,对象的形式 function change2() { User.$patch({ name: &amp;#39;雪见&amp;#39;, age: 17 }) } // 方式三,可一次性修改多个值,函数的形式 function change3() { User.$patch((state) =&gt; { state.name = &amp;#39;徐长卿&amp;#39; state.age = 19 }) } // 方式四,哪怕修改一个值,也要传所有值??? function change4() { User.$state = { name: &amp;#39;茂茂&amp;#39;, age: 16, name1: &amp;#39;李逍遥&amp;#39;, age1: 18 } } // 方式五,借助actions function change5() { User.setAge() //也可以传参 // User.setAge(999) } // 重置数据 function handleReset() { User.$reset() } &lt;/script&gt; &lt;style&gt; &lt;/style&gt;</pre> stored in pinia

The above is the detailed content of How to use Vue3 pinia state management tool. For more information, please follow other related articles on the PHP Chinese website!

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