惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
有赞技术团队
有赞技术团队
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
雷峰网
雷峰网
T
Tor Project blog
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
J
Java Code Geeks
AWS News Blog
AWS News Blog
Security Latest
Security Latest
Spread Privacy
Spread Privacy
T
Threatpost
博客园 - 三生石上(FineUI控件)
I
Intezer
G
Google Developers Blog
Scott Helme
Scott Helme
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
A
Arctic Wolf
F
Full Disclosure
P
Proofpoint News Feed
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
B
Blog
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
Know Your Adversary
Know Your Adversary

liaooo

react-router在组件外操作路由 – liaooo Vue3自动导入常用api – liaooo Vue3自定义组件名字 – liaooo Vue+ts使用animate.css – liaooo uni-app中flyio的使用 – liaooo 【开源】查看网站在不同设备的预览效果 – liaooo craco配置postcss不生效 – liaooo Vite配置preprocessorOptions全局引入less – liaooo 监听Pinia仓库中的数据变化 – liaooo
Pinia的使用 – liaooo
liao · 2023-07-09 · via liaooo

安装

yarn add pinia
# or
npm i pinia

基本使用

在main.js中挂载Pinia

import { createPinia } from 'pinia'
const Pinia = createPinia()

createApp(App).use(Pinia).mount('#app')

新建仓库文件

src/test.js

引入defineStore方法
import { defineStore } from 'pinia'
创建store

命名规则:useXxxStore

defineStore( storeID , {state,actions,getters} )

参数1:store的唯一标识,也就是仓库名字

参数2:对象,可以在对象内提供state、actions、getters

const useTestStore = defineStore('test', {
  // state是一个函数 在返回的对象中写数据
    state: () => {
        return {
            name: 'liao',
            website: 'https://liaooo.cn',
        }
    },
    actions: {},
    getters: {}
})

export default useTestStore
在组件中使用
<script setup>
    import useTestStore from './store/test'
    const test = useTestStore()
</script>

<template>
    {{ test.name }}
    {{ test.website }}
</template>

actions的使用

Pinia中没有mutations,只有actions

在actions中添加方法并修改数据

...
actions: {
        changeName(name) {
            this.name = name
        }
  }
...

在组件中使用actions

<script setup>
    import useTestStore from './store'
    const test = useTestStore()
  // 执行actions中的方法
    test.changeName('liaoliao')
</script>

getters的使用

添加getter

...
getters: {
        infoPage() {
            return this.website + '/talking'
        },
    }
...

在组件中使用

<template>
    ...
    {{ test.infoPage }}
</template>

<script setup>
    import useTestStore from './store'
    const test = useTestStore()
    ...
</script>

storeToRefs的使用

直接解构store中的数据会导致数据丢失响应式特性

<script setup>
    import useTestStore from './store'
    const test = useTestStore()
  // 解构出来的数据将不再是响应式的
    const { name, website } = test
    ...
</script>

使用storeToRefs可以使解构出来的数据依旧是响应式的

<script setup>
  // 从pinia中引入storeToRefs方法
  import { storeToRefs } from 'pinia'
    ...
    const { name, website } = storeToRefs(test)
    ...
</script>

模块化

根据项目复杂程度,有时候需要存储较多数据,可以定义多个store,最后统一用一个根store整合

新建user模块

src/store/user.js

import { defineStore } from 'pinia'

const useUserStore = defineStore('user', {})

export default useUserStore

新建······模块

新建根store

src/store/index.js

// 导入所有store
import useUserStore from './user'
import use···Store from './···'

// 统一导出useStore()方法
export default function useStore() {
  return {
    user: useUserStore(),
     ···: use···Store()
  }
}

在组件中使用

<script setup>
    import { storeToRefs } from 'pinia'
    import useStore from './store'
  // 解构user store
    const { user } = useStore()
    const { name, pwd } = storeToRefs(user)
</script>

订阅store中数据的变化($subscribe)

store.$subscribe详见下一篇笔记