• Pinia 计数器


    新增stores目录

    在stores目录里面新增一个counter.js 文件

    文件代码:

    1. import { defineStore } from 'pinia
    2. export const useCounterStore = defineStore('counter',{
    3. state:()=>{
    4. return { count:0 }
    5. },
    6. //也可以这样定义
    7. //state:() => ({ count : 0})
    8. actions: {
    9. increment() {
    10. this.count++
    11. }
    12. }
    13. })

    在组件中使用

    1. import { useCounterStore } from '@stores/counter'
    2. export default {
    3. setup() {
    4. const counter = useCounterStore() //初始化 counter对象
    5. counter.count++
    6. //带有自动补全
    7. counter.$patch({ count : counter.count+1 })
    8. //或者用action代替
    9. counter.increment()
    10. }
    11. }

    或者更高级的定义store

    1. //定义store
    2. import { defineStore } from 'pinia'
    3. import { ref } from 'vue'
    4. export const useCounterStore = defineStore ('counter',()=>{
    5. //数据(state)
    6. const count = ref(0)
    7. //修改数据的方法 (action)
    8. const increment = () =>{
    9. count.value++
    10. }
    11. //以对象形式返回
    12. return {count . increment}
    13. })

    然后在组件里使用Store

    1. <script setup>
    2. //1.导入'useCounterStore' 方法
    3. import { useCounterStore } from '@/stores/counter'
    4. //2.执行方法得到counterStore对象
    5. const counterStore = useCounterStore()
    6. </script>
    7. <template>
    8. <button @click = "counterStore.increment">
    9. {{ counterStore.count }}
    10. </button>
    11. </template>

    总结:

    1、定义store (使用导入的useCounterStore)

    2、组件中使用

    vuex中getters为计算属性

    getters在pinia中实现,直接使用computed函数进行模拟

    1. // 数据 (state)
    2. const count = ref(0)
    3. //getter
    4. const doubleCount = computed(() => count.value * 2)

    在state里面加入getter

    1. //定义store
    2. import { defineStore } from 'pinia'
    3. import { ref } from 'vue'
    4. export const useCounterStore = defineStore ('counter',()=>{
    5. //数据(state)
    6. const count = ref(0)
    7. //修改数据的方法 (action) 同步+异步
    8. const increment = () =>{
    9. count.value++
    10. }
    11. //getter定义
    12. const doubleCount = computed(() => count.value*2)
    13. //以对象形式返回 供组件使用
    14. return {count,
    15. increment,
    16. doubleCount //return 对象里加入
    17. }
    18. })
    1. <script setup>
    2. //1.导入 use 打头的方法
    3. import { useCounterStore } from '@/store/counter'
    4. //2.执行方法得到store实例对象
    5. const counterStore  = useCounterStore()
    6. console.log(counterStore)
    7. </script>
    8. <template>
    9. <button @click = "counterStore.increment"> {{counterStore.count}} </button>
    10. {{ counterStore.doubleCount }}
    11. </template>

    异步action

    //定义异步 action

     const list = ref([])

    const getlist = async ()=>{

       const res = await axios.get(API_URL)

    }

    同理要在return 里返回

    1. //定义store
    2. import { defineStore } from 'pinia'
    3. import { ref } from 'vue'
    4. export const useCounterStore = defineStore ('counter',()=>{
    5. //数据(state)
    6. const count = ref(0)
    7. //修改数据的方法 (action) 同步+异步
    8. const increment = () =>{
    9. count.value++
    10. }
    11. //定义异步 action
    12. const list = ref([])
    13. const getList = async ()=>{
    14.     const res = await axios.get(API_URL)
    15. }
    16. //getter定义
    17. const doubleCount = computed(() => count.value*2)
    18. //以对象形式返回 供组件使用
    19. return {count,
    20. increment,
    21. doubleCount, //return 对象里加入
    22. getList
    23. }
    24. })

    不用对象+.的形式取 用解构赋值

    但是一般的解构赋值会丢失响应式 那么应该怎么办呢

    1. import { storeToRefs } from 'pinia'
    2. import { useCounterStore } from '@/stores/counter'
    3. const counterStore = useCounterStore
    4. //然后用这个方法包裹一下(保持响应式更新)
    5. const {count , doubleCount} = storeToRefs(counterStore)

    包裹后产出的是两个ref对象,而不是解构的值

    那么方法能不能解构赋值 (响应式) 方法要用一般解构赋值的写法才可以 不能用storeToRefs方法包裹

    Pinia调试

    总结:

    1、Pinia 是用来做什么的?

    集中状态管理工具,新一代的vuex

    2、Pinia中还需要mutation吗?

    不需要,action既支持同步也支持异步

    3、Pinia如何实现getter

    computed计算属性函数

    4、Pinia产生的Store如何解构赋值数据保持响应式?

    storeToRefs

  • 相关阅读:
    机器学习必修课 - 编码分类变量 encoding categorical variables
    微信小程序毕业设计开题报告_springboot音乐网站
    Java面试题-线程
    MySQL高级篇知识点——数据库其它调优策略
    进阶:编写符合Python风格的对象
    教程八 在Go中使用Energy创建跨平台GUI应用 - Go执行JS函数
    基于Vue3水印组件封装:防篡改守护!
    在Mac中使用 brew services start redis 命令启动、停止Redis服务报错
    面试经验——面试项目准备工作
    Spring核心扩展点BeanDefinitionRegistryPostProcessor源码分析
  • 原文地址:https://blog.csdn.net/qq_53478650/article/details/134281721