• vue3 状态管理工具 pinia 使用


    1、安装pinia

    npm install pinia --save

    2、创建 Store

    新建一个 store 文件,创建 index.ts

    1. import { createPinia } from "pinia";
    2. const store = createPinia();
    3. export default store;

    3、在main.ts里全局引入

    1. import { createApp } from "vue";
    2. import App from "./App.vue";
    3. import router from "./router";
    4. import store from "./store"; // 引入
    5. const app = createApp(App);
    6. app.use(router);
    7. app.use(store); // 注册
    8. app.mount("#app");

    4、定义一个store

    在 store 文件里建一个 counter.ts ,定义该状态管理,可根据自己项目建多个

    1. import { defineStore } from "pinia";
    2. export const useCounterStore = defineStore("counter", {
    3. state: () => { // 数据
    4. return {
    5. count: 0,
    6. msg: "我cao",
    7. todoList: ["111", "222"],
    8. };
    9. },
    10. getters: {
    11. doubleCount(state: any): number { // 计算
    12. return state.count * 2;
    13. },
    14. },
    15. actions: {
    16. increment(payload?: number) { // 方法
    17. this.count = payload ? this.count + payload : this.count + 1;
    18. },
    19. },
    20. });

    5、访问State

    1. <template>
    2. <div class="home">
    3. <p>{{ counterStore.msg }}</p>
    4. <p>{{ counterStore.count }}</p>
    5. <p>{{ counterStore.doubleCount }}</p>
    6. <ul>
    7. <li v-for="(item, index) in todoList" :key="index">{{ item }}</li>
    8. </ul>
    9. </div>
    10. </template>
    11. <script setup lang="ts">
    12. import { useCounterStore } from "@/store/counter"; // 引入
    13. const counterStore = useCounterStore(); // 使用
    14. </script>

    你也可以使用storeToRefs进行解构  

    1. <template>
    2. <div class="about">
    3. <p>{{ count }}</p>
    4. <p>{{ msg }}</p>
    5. <p>{{ doubleCount }}</p>
    6. <ul>
    7. <li v-for="(item, index) in todoList" :key="index">{{ item }}</li>
    8. </ul>
    9. </div>
    10. </template>
    11. <script setup lang="ts">
    12. import { storeToRefs } from 'pinia'
    13. import { useCounterStore } from '@/stores/counter'
    14. const counterStore = useCounterStore()
    15. // 也可以使用storeToRefs进行解构
    16. let { count, doubleCount, msg, todoList } = storeToRefs(counterStore)
    17. </script>

    defineComponent 模式下的使用

    1. <template>
    2. <div class="hello">
    3. <p>{{ counterStore.msg }}</p>
    4. <p>{{ counterStore.count }}</p>
    5. <p>{{ counterStore.doubleCount }}</p>
    6. <p>{{ counterStore }}.........</p>
    7. <p @click="test">直接修改</p>
    8. </div>
    9. </template>
    10. <script lang="ts">
    11. import { defineComponent } from "vue";
    12. import { useCounterStore } from "@/store/counter"; // 引入
    13. export default defineComponent({
    14. name: "HelloWorld",
    15. props: {
    16. msg: String,
    17. },
    18. setup() {
    19. const counterStore = useCounterStore(); // 调用
    20. const test = () => {
    21. counterStore.count++;
    22. };
    23. return {
    24. counterStore, // 返回
    25. test,
    26. };
    27. },
    28. });
    29. </script>

    6、修改状态数据 

    1、直接修改和批量修改

    1. <template>
    2. <div class="home">
    3. <p>{{ counterStore.count }}</p>
    4. <p @click="test">直接修改</p>
    5. </div>
    6. </template>
    7. <script setup lang="ts">
    8. import { useCounterStore } from "@/store/counter";
    9. const counterStore = useCounterStore();
    10. const test = () => {
    11. counterStore.count++; // 直接修改
    12. };
    13. </script>

    2、批量修改  $patch

    1. <template>
    2. <div class="home">
    3. <p>{{ counterStore.msg }}</p>
    4. <p>{{ counterStore.count }}</p>
    5. <p>{{ counterStore.doubleCount }}</p>
    6. <p @click="test2">批量修改</p>
    7. </div>
    8. </template>
    9. <script setup lang="ts">
    10. import { useCounterStore } from "@/store/counter";
    11. const counterStore = useCounterStore();
    12. const test2 = () => {
    13. // 批量修改
    14. counterStore.$patch((state) => {
    15. state.count = 10000,
    16. state.msg = '你好',
    17. state.todoList.push('看看')
    18. })
    19. };
    20. </script>

    3、重置 $reset ,可把参数重置回原始参数

    counterStore.$reset()

    4、替换 $state ,也只能换原有的参数,和批量修改差不多

    counterStore.$state = { counter: 666, msg: "Paimon", todoList: [] };

    5、监听 state 数据变化 

    1. counterStore.$subscribe((mutation: any, state: any) => {
    2. // 订阅 state 值的变化
    3. console.log("cart:" + JSON.stringify(state));
    4. });

  • 相关阅读:
    产学交流 | 重庆理工大学计算机科学与工程学院一行到访芝诺数据
    基因家族特征分析 - 染色体定位分析
    【工作总结】工作为什么总是手忙脚乱
    如何在Linux上部署web程序
    Leetcode.4 寻找两个正序数组的中位数
    小数第n位【蓝桥杯】
    LuatOS-SOC接口文档(air780E)--mcu - 封装mcu一些特殊操作
    Scala数据结构
    ctf web基础php
    docker安装flink
  • 原文地址:https://blog.csdn.net/qq_25186543/article/details/126869580