VUE3 + Pinia

 一、状态管理库Pinia

  1.介绍
介绍
Pinia是vue的状态管理库,是Vuex状态管理库的替代
​
优势
1.提供更加简单的API(去掉了mutation)
2.提供符合组合式风格的API
3.去掉了modules的概念,每个store都是一个独立的模块
4.搭配TypeScript一起使用提供可靠的类型推断
  2.使用
    2.1.安装
npm install pinia
    2.2创建一个 pinia 实例
//在main.js文件
import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
// 导入createPinia
import { createPinia } from 'pinia'
// 创建实例
const pinia = createPinia()
// 挂载pinia到app应用中
createApp(App).use(pinia).mount('#app')
  3.定义Store(state+action)
//在Stores文件夹下创建counter.js文件

// 导入一个方法 defineStore
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
​
export const useCounter = defineStore('counter', () => {
  //定义数据(state)
  const count = ref(0)
  // 定义修改数据的方法(action 同步和异步)
  const increment = () => {
   count.value += 1
  }
  // getter定义
  const getterCount = computed(() => count.value * 2)
   // 以对象的方法return提供组件使用
  return {
   count,
   increment,
   getterCount
  }
​
})
​
  4.组件使用Store
<script setup>
    // 导入pinia
    import { useCounter } from '@/Stores/counter.js'
    const Counter = useCounter()
</script>
<template>
     <button @click="Counter.increment">{{ Counter.count }}</button>
     <div>
       {{ Counter.getterCount }}
     </div>
</template>
​
<style scoped></style>
​
    5.storeToRefs       
      5.1介绍
使用storeToRefs函数可以辅助保持数据(state+getter)的响应式解构
      5.2使用
定义
const { 解构数据 } = storeToRefs(Counter)
​
使用
<script setup>
    // 导入pinia
    import { useCounter } from '@/Stores/counter.js'
    import { storeToRefs } from 'pinia';
    const Counter = useCounter()
    //解构赋值保存响应式数据
    const { count, getterCount } = storeToRefs(Counter)
    // 方法的解构赋值
    const { increment } = Counter
    console.log(Counter);
​
</script>
<template>
    <button @click="increment">{{ count }}</button>
    <div>
       {{ getterCount }}
     </div>
</template>
  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值