-
使用 defineStore 创建一个 store, 每个 store 要设置一个唯一 id;
import { defineStore } from 'pinia' import { ref } from 'vue' // useStore 可以是 useUser、useCart 之类的任何东西 // 第一个参数是应用程序中 store 的唯一 id export const useMainStore = defineStore('main', { // other options... const name = ref('cherish') const count = ref(0) return { name, count } })
-
改变state 的值时不需要借助 mutation (pinia中没有mutation), 默认情况下,您可以通过
store
实例访问状态来直接读取和写入状态;const mainStore = useMainStore() mainStore.count++
-
除了直接用
store.count++
修改 store,你还可以调用$patch
方法同时应用多个更改;$patch
方法也接受一个函数来批量修改集合内部分对象的情况;const mainStore = useMainStore() mainStore.$patch({ counter: mainStore.counter + 1, name: 'yb.z', })
-
useMainStore 中的返回值,不能使用解构语法,否则会失去响应性,但是可以使用 storeToRefs 包裹 store,然后可以使用解构;
import { storeToRefs } from 'pinia' const mainStore = useMainStore() // 正确 const { name } = useMainStore() // 错误 const { name } = storeToRefs(useMainStore()) // 正确
-
通过调用 store 上的
$reset()
方法将状态 重置 到其初始值;const mainStore = useMainStore() mainStore.name = 'yb.z' mainStore.$reset() // mainStore.name cherish
-
可以通过将其
$state
属性设置为新对象来替换 Store 的整个状态;const mainStore = useMainStore() mainStore.$state = { counter: 666, name: 'yb.z' }
-
可以通过 store 的
$subscribe()
方法查看状态及其变化, 与常规的watch()
相比,使用$subscribe()
的优点是 subscriptions 只会在 patches 之后触发一次; -
使用getter 和 直接在状态中使用 computed 计算的效果一样;
-
如果一个 getter 依赖另一个 getter,通过
this
访问任何其他 getter;export const useStore = defineStore('main', { state: () => ({ counter: 0, }), getters: { // 类型是自动推断的,因为我们没有使用 `this` doubleCount: (state) => state.counter * 2, // 这里需要我们自己添加类型(在 JS 中使用 JSDoc)。 我们还可以 // 使用它来记录 getter /** * 返回计数器值乘以二加一。 * * @returns {number} */ doubleCountPlusOne() { // 自动完成 return this.doubleCount + 1 }, }, })
-
同 computed 属性一样,getter 默认是不能传递参数进去的,但是,您可以从 getter 返回一个函数以接受任何参数,此时 getter 变成了一个普通函数,不再缓存数据。
-
要使用其他 store 的 getter, 可以直接在一个 store 中引入和使用另一个 store;
import { useOtherStore } from './other-store' export const useStore = defineStore('main', { state: () => ({ // ... }), getters: { otherGetter(state) { const otherStore = useOtherStore() return state.localData + otherStore.data }, }, })
-
getter 的访问和 state 一模一样,都是 xxxStore.xxx;
-
与 getter 一样,actions 操作可以通过
this
访问整个 store 实例,不同的是,action
可以是异步的,当然也可以是同步的,你可以在它们里面await
调用任何 API,以及其他 action!可以把 action 理解为普通的方法; -
想要使用另一个 store 的action的话,可以直接在一个 store 中引入和使用另一个 store, 然后直接在 action 中调用就好了另一个 store 的action 就好了。
import { useAuthStore } from './auth-store' export const useSettingsStore = defineStore('settings', { state: () => ({ preferences: null, // ... }), actions: { async fetchUserPreferences() { const auth = useAuthStore() if (auth.isAuthenticated) { this.preferences = await fetchPreferences() } else { throw new Error('User must be authenticated') } }, }, })
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
暂无评论内容