Pinia基本使用示例

1. 定义Store【src\stores\counter.ts】。
(1). 组合式API写法。

import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }

  return { count, doubleCount, increment }
})

(2). 类似Vuex的写法。

import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state:()=>({count:0}), // 相当于 state:()=>{ return {count:0}; }。
  getters:{
    doubleCount:state=>state.count * 2,
  },
  actions:{
    increment(){
      this.count++;
    }
  }
})

2. 使用Store【src\views\ChildComponent.vue】。
(1). 组合式API写法。

<script setup>
import { useCounterStore } from '@/stores/counter'

const counter = useCounterStore();

function reset(){
    // 三种改变state的方法展示。
    counter.count=0; // 直接变更。
    // counter.$patch(state=>{state.count=0}) // 传入箭头函数来变更。
    // counter.$patch({count:0}) // 传入新的state对象来变更。
}
</script>
<template>
  <div>
    <div @click="counter.increment()">{{ counter.count }}、{{ counter.doubleCount }}</div>
        <button @click="reset()">reset</button>
  </div>
</template>

(2). 组合式API写法 中 使用解构语法。

<script setup>
import { storeToRefs } from 'pinia';
import { useCounterStore } from '@/stores/counter'

const counter = useCounterStore();

let { count,doubleCount } = storeToRefs(counter);
const { increment } = counter;

function reset(){
	// 四种改变state的方法展示。
	count.value = 0; // 需要通过.value来改变响应式ref的值,这样写能够直接改变counter中state count的值。
	// counter.count=0;
	// counter.$patch(state=>{state.count=0}) // 传入箭头函数来变更。
	// counter.$patch({count:0}) // 传入新的state对象来变更。
}
</script>
<template>
  <div>
    <div @click="increment()">{{ count }}、{{ doubleCount }}</div>
		<button @click="reset()">reset</button>
  </div>
</template>

(3). 选项式API写法。

<script>
import {mapState,mapActions,mapStores} from 'pinia'; // mapGetters,
import {useCounterStore} from '@/stores/counter';
export default{
    computed:{
        ...mapStores(useCounterStore), // 如果还有其它Stores要引入,那么就在useCounterStore后面加上“,其它Stores”。
        ...mapState(useCounterStore,['count','doubleCount']), // mapState现在返回的接口 已经 包含getters了。
        // ...mapGetters(useCounterStore,['doubleCount']), // 已经不推荐使用mapGetters了。
    },
    methods:{
        ...mapActions(useCounterStore,['increment']),
        reset(){
            this.counterStore.count = 0;
            // this.counterStore.$patch({count:0});
            // this.counterStore.$patch(state=>{state.count=0});
        }
    }
}
</script>

<template>
  <div>
        <div @click="increment()">{{ count }}、{{ doubleCount }}</div>
        <button @click="reset()">reset</button>
  </div>
</template>

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值