pinia修改状态数据的方式
# pinia修改状态数据的方式
# 1、$patch修改多条数据
组件:AddButton.vue
<template>
<div><button @click="handleClick">Add handleClick</button></div>
<div><button @click="handleClickPatch">Add handleClickPatch</button></div>
</template>
<script setup>
import { mainStore } from '../stores/counter';
const store = mainStore()
const handleClick = () => {
store.count++;
// store.helloWorld = store.helloWorld === 'yq' ? 'helloworld' : 'yq'
}
// 修改状态数据 $patch 可同时修改多个数据的状态
const handleClickPatch = () => {
store.$patch({
count:store.count + 2,
helloWorld:store.helloWorld === 'yq' ? 'HelloWorld' : 'yq'
})
}
</script>
<style lang="scss" scoped>
</style>