Appearance
State
访问 State
默认情况下,你可以通过 store
实例访问 state,直接对其进行读写。
js
const store = useStore()
store.count++
注意,新的属性如果没有在 state()
中被定义,则不能被添加。它必须包含初始状态。例如:如果 secondCount
没有在 state()
中定义,我们无法执行 store.secondCount = 2
。
重置 State
使用选项式 API 时,你可以通过调用 store 的 $reset()
方法将 state 重置为初始值。
js
const store = useStore()
store.$reset()
在 $reset()
内部,会调用 state()
函数来创建一个新的状态对象,并用它替换当前状态。
在 Setup Stores 中,您需要创建自己的 $reset()
方法:
js
export const useCounterStore = defineStore('counter', () => {
const count = ref(0)
function $reset() {
count.value = 0
}
return { count, $reset }
})
使用选项式 API 的用法
如果你不能使用组合式 API,但你可以使用 computed
,methods
,...,那你可以使用 mapState()
辅助函数将 state 属性映射为只读的计算属性:
js
import { mapState } from 'pinia'
import { useCounterStore } from '../stores/counter'
export default {
computed: {
// 可以访问组件中的 this.count
// 与从 store.count 中读取的数据相同
...mapState(useCounterStore, ['count'])
// 与上述相同,但将其注册为 this.myOwnName
...mapState(useCounterStore, {
myOwnName: 'count',
// 你也可以写一个函数来获得对 store 的访问权
double: store => store.count * 2,
// 它可以访问 `this`,但它没有标注类型...
magicValue(store) {
return store.someGetter + this.count + this.double
},
}),
},
}
可修改的 state
如果你想修改这些 state 属性 (例如,如果你有一个表单),你可以使用 mapWritableState()
作为代替。但注意你不能像 mapState()
那样传递一个函数:
js
import { mapWritableState } from 'pinia'
import { useCounterStore } from '../stores/counter'
export default {
computed: {
// 可以访问组件中的 this.count,并允许设置它。
// this.count++
// 与从 store.count 中读取的数据相同
...mapWritableState(useCounterStore, ['count'])
// 与上述相同,但将其注册为 this.myOwnName
...mapWritableState(useCounterStore, {
myOwnName: 'count',
}),
},
}
变更 state
除了用 store.count++
直接改变 store,你还可以调用 $patch
方法。它允许你用一个 state
的补丁对象在同一时间更改多个属性:
js
store.$patch({
count: store.count + 1,
age: 120,
name: 'DIO',
})
$patch
方法也接受一个函数来组合难以用补丁对象实现的变更:
js
store.$patch((state) => {
state.items.push({ name: 'shoes', quantity: 1 })
state.hasChanged = true
})
替换 state
你不能完全替换掉 store 的 state,因为那样会破坏其响应性。但是,你可以 patch 它。
js
// 这实际上并没有替换`$state`
store.$state = { count: 24 }
// 在它内部调用 `$patch()`:
store.$patch({ count: 24 })
你也可以通过变更 pinia
实例的 state
来设置整个应用的初始 state。这常用于 SSR 中的激活过程。
js
pinia.state.value = {}
订阅 state
你可以通过 store 的 $subscribe()
方法侦听 state 及其变化
js
cartStore.$subscribe((mutation, state) => {
// import { MutationType } from 'pinia'
mutation.type // 'direct' | 'patch object' | 'patch function'
// 和 cartStore.$id 一样
mutation.storeId // 'cart'
// 只有 mutation.type === 'patch object'的情况下才可用
mutation.payload // 传递给 cartStore.$patch() 的补丁对象。
// 每当状态发生变化时,将整个 state 持久化到本地存储。
localStorage.setItem('cart', JSON.stringify(state))
})
刷新时机
在底层实现上,$subscribe()
使用了 Vue 的 watch()
函数。你可以传入与 watch()
相同的选项。当你想要在 每次 state 变化后立即触发订阅时很有用:
js
cartStore.$subscribe((mutation, state) => {
// 每当状态发生变化时,将整个 state 持久化到本地存储
localStorage.setItem('cart', JSON.stringify(state))
}, { flush: 'sync' })
取消订阅
默认情况下,state subscription 会被绑定到添加它们的组件上 (如果 store 在组件的 setup()
里面)。这意味着,当该组件被卸载时,它们将被自动删除。如果你想在组件卸载后依旧保留它们,请将 { detached: true }
作为第二个参数,以将 state subscription 从当前组件中分离:
js
<script setup>
const someStore = useSomeStore()
// 此订阅器即便在组件卸载之后仍会被保留
someStore.$subscribe(callback, { detached: true })
</script>
TIP
你可以在 pinia
实例上使用 watch()
函数侦听整个 state。
js
watch(
pinia.state,
(state) => {
// 每当状态发生变化时,将整个 state 持久化到本地存储。
localStorage.setItem('piniaState', JSON.stringify(state))
},
{ deep: true }
)