梗概
- 概念上和Vue computed一样
实例
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0,
}),
getters: {
// automatically infers the return type as a number
doubleCount(state) {
return state.count * 2
},
// the return type **must** be explicitly set
doublePlusOne(): number {
// autocompletion and typings for the whole store ✨
return this.doubleCount + 1
},
getUserById: (state) => {
return (userId) => state.users.find((user) => user.id === userId)
},
},
})访问方式:
- 直接访问状态实例的doubleCount属性
- 直接访问状态实例的getUserById()函数