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

推荐订阅源

N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
D
Docker
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
T
Tenable Blog
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
The Hacker News
The Hacker News
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threatpost
V
Visual Studio Blog
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Scott Helme
Scott Helme
A
About on SuperTechFans
WordPress大学
WordPress大学
F
Fortinet All Blogs
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
Latest news
Latest news
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Schneier on Security

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