• pinia不同于vuex的状态管理器


    用过pinia再也不想用vuex了

    1. 安装
      yarn add pinia
      npm install pinia
    2. 在main.ts中集成
      const pinia = createPinia()
      const app = createApp(App)
      app.use(pinia) // 核心,注入插件

    经过上述步骤以及可以使用pinia

    • 直接上手,采用自定义的模式使用pinia,想象一下,把vuex的所有功能(state,actions…)完全想平常写代码一样,实现,不必拘束于state,actions,mutations等属性。
    import { defineStore } from "pinia";//引入pinia,必填
    export const useMyPinia = defineStore('myPinia',() => {
      // myPinia,是必须的,即使唯一的标识也是连接到devtools的标识。
      // useMyPinia约点俗称我们应写成usexxx
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 好了,至此pinia的商店已经搭建完毕,下一步在商店里实现vuex的所有功能。
    import { defineStore } from 'pinia'
    import { ref } from 'vue'
    export const useMyPinia = defineStore('myPinia',() => {
            let pinia = reactive({// 我们完全可以使用响应式,就像写普通的setup函数一样。
                name:'pinia商店',
                fruits:'暂时没有水果'
            })  // 参数pinia可以理解为vuex的state
            function add(){ // 可以理解为actions,mutatios的集合,
            //可以像action一样进行一些异步操作,如ajax请求。
            // 可以像mutations一样修改state的状态。
                pinia.fruits = 'apple'
            }
            return {
                pinia,add // 返回值必须,就像setup一样。
            }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 在组件中使用他们。
    <template>
        <div>
            <h1>{{myPinia.pinia.name}}</h1>
            <el-button @click="myPinia.add">增加水果</el-button>
            <div>{{myPinia.pinia.fruits}}</div>
        </div>
    </template>
    <script lang="ts" setup>
    import { useMyPinia } from '../../../../store/js';
    const myPinia = useMyPinia()
    // myPinia.pinia.name结果为 pinia商店
    //	myPinia.pinia.fruits 结果为 暂时没有水果
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 初始效果如下
      在这里插入图片描述

    • 当我们点击按钮的时候,在add函数中
      pinia.fruits = 'apple'
      在这里插入图片描述

    • pinia的基本功能已经实现了!

    在组件中直接修改state的数据

    myPinia.$patch(state => { state.pinia.fruits = 'orange' })
    就是这么简单,pinia中的fruits就变成orange了,打开开发者工具。
    在这里插入图片描述
    可以看到,数据直接被修改。

    • 此刻已经可以用pinia完成vuex的所有功能了!

    如果还想用state,actons,getters,那么pinia也提供了对应的属性。

    export const useOtherPinia = defineStore('otherPinia',{
        state: () => (
            {
                person:{
                    name:'小明',
                    age:18
                }
            }
        ),
        actions:{
            add(){
                this.person.name = '小红'
            }
        },
        getters:{
            count: state => state.person.age * 10
        }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 在组件的使用方法和自定义pinia属性一模一样,这里不在赘述。
  • 相关阅读:
    【JAVA】-- 简易超市管理系统(二)(实现思路+每步代码)
    TensorFlow 的基本概念和使用场景
    LeetCode第13题:罗马数字转整数
    【Java基础】面向对象基础
    【沐风老师】怎么在3DMAX中使用MAXScript脚本动画编程?
    Win11电脑摄像头打开看不见,显示黑屏如何解决?
    【踩坑专栏】0%classes,0% lines covered
    [Spring笔记] SpringMVC-04-get请求与post请求发送普通参数
    scrapy框架流程
    Pytorch实战教程(二)-PyTorch基础
  • 原文地址:https://blog.csdn.net/qq_46433453/article/details/126248318