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

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

liaooo

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

当仓库中state数据发生变化时,可以通过$subscribe进行监听

<script setup>
    import { storeToRefs } from 'pinia'
    import useStore from './store'
    const { test } = useStore()
    const { name, website } = storeToRefs(test)
  // 监听仓库数据变化
    const subscribe = test.$subscribe((mutation, state) => {},{})

  // 触发action
    test.changeName('liaoliao')
</script>

详解

store.$subscribe((mutation, state)=>{} , {})

$subscribe()有两个参数

第一个参数

回调函数 函数带有两个参数mutationstate

mutation

对象 有三个属性 eventsstoreIdtype

events 包含state变化前后的数据

{
    "effect": {
                     ...
    },
    "target": {
        "name": "liaoliao",
        "website": "https://liaooo.cn"
    },
    "type": "set",
    "key": "name",
    "newValue": "liaoliao",
    "oldValue": "liao"
}

storeId

当前仓库的名字

storeID: "test"

type

用什么方式触发变化

type: "direct"
// "direct" 通过 action 变化的
// "patch object" 通过 $patch 传递对象的方式改变的
// "patch function"  通过 $patch 传递函数的方式改变的

第二个参数

对象 一些参数配置

{
  detached: false,
  immediate:false,
  deep:false,
  flush:'···',
}

detached

布尔值

默认false 订阅所在的组件被卸载时,订阅停止

如果设置detached值为true 时,即使所在组件被卸载,订阅依然生效

主动停止订阅

const subscribe = test.$subscribe((mutation, state) => {},{})
// 调用变量主动停止订阅
subscribe()

Pinia

https://pinia.vuejs.org/core-concepts/state.html#subscribing-to-the-state

Vuex

https://vuex.vuejs.org/zh/api/index.html#subscribe