当前位置: 首页 > news >正文

vue3 vite 两种监听pinia状态变化的方式比较:watch, $subscribe

首先搭建vue3 vite 项目

npm create vue

选择pinia 或者自己安装pinia

自己安装需要

npm install pinia

并在main.js中挂在上:

const pinia = createPinia()
const app = createApp(App)
app.use(pinia)
app.mount('#app')

创建stores文件夹和counter.js文件

在这里插入图片描述

counter.js中的内容如下:

import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', () => {
    const count = ref(0)
    const other_counts = ref(0)
    
    const doubleCount = computed(() => count.value * 2)
    function increment() {
        count.value++
        console.log(count.value)
    }
    // 分别设置不同的计时器
    setInterval(()=>{
        count.value = count.value+1
    },1000)
    
    // 分别设置不同的计时器
    setInterval(()=>{
        other_counts.value = other_counts.value+1
    }, 3000)
	// 一定要返回 count, other_counts 不然监听不到
    return { count, other_counts, doubleCount, increment }
})

在app.vue中进行监听,内容如下:

import {watch} from "vue";
import {useCounterStore} from "@/stores/counter.js";

const counter_store = useCounterStore()
// 单独监听store中的某一个属性
watch(()=>counter_store.count, (new_data, old_data)=>{
  console.log("watch", old_data, new_data)
})

// // 监听 store 的状态变化, 即监听store中的所有属性的变化
counter_store.$subscribe((mutation, state) => {
  console.log("state.count", state.count)
  console.log("state.other_counts", state.other_counts)
});

页面打印结果如下:

watch 0 1
state.count 1
state.other_counts 0
watch 1 2
state.count 2
state.other_counts 0
watch 2 3
state.count 3
state.other_counts 0
state.count 3                   // 这次输出的3 是因为other_counts的状态发生了变化才输出state.count
state.other_counts 1   
watch 3 4
state.count 4
state.other_counts 1

打印结果分析:

watch监听到store中某一个属性变化后会立即响应,$subscribe方式会监听整个store中的所有属性是否变化,任何一个变化,都会调用$subscribe里的回调函数

$subscribe 也可以这样写:

import {watch} from "vue";
import {useCounterStore} from "@/stores/counter.js";

const counter_store = useCounterStore()

// watch(()=>counter_store.count, (new_data, old_data)=>{
//   console.log("watch", old_data, new_data)
// })

// const websocketMsessage = computed(() => websocketMsessageStore.websocketMsessage);
// // 监听 store 的状态变化
counter_store.$subscribe((mutation, {other_counts}) => {
  // console.log("state.count", state.count)
  console.log("state.other_counts",other_counts)
});

打印结果如下:

在这里插入图片描述
再次说明了$subscribe只要监听到store中任何字段的变化都会执行其回调函数

相关文章:

  • rust编程实战:实现3d粒子渲染wasm
  • Leetcode 112: 路径总和
  • diffuser库使用本地模型生成图像
  • MagicDriveDiT:具有自适应控制的自动驾驶高分辨率长视频生成
  • 树莓集团南京新项目:百度百科更新背后的战略意图
  • C 语 言 --- 猜 数 字 游 戏
  • MDM 如何彻底改变医疗设备的远程管理
  • 二、Java-封装playwright UI自动化(根据官网执行步骤,首先封装BrowserFactory枚举类及BrowserManager)
  • 【leetcode hot 100 73】矩阵置零
  • 软件高级架构师 - 软件工程
  • VS2022安装Framework 4.0和.NET Framework 4.5
  • Android车机DIY开发之软件篇(二十)立创泰山派android编译
  • C语言100天练习题【记录本】
  • C++ Boost库中Chrono时间模块的基本使用
  • GB28181开发--ZLMediaKit‌+WVP+Jessibuca‌
  • JAVA入门——反射
  • Sass 模块化革命:深入解析 @use 语法,打造高效 CSS 架构
  • 解锁访问者模式:Java编程的灵活之道
  • 神旗视讯Linux client 3.4版本发布和开源
  • 每日学习Java之一万个为什么?(Maven篇+RPC起步+CICD起步)(待完善)
  • 首次带人形机器人走科技节红毯,傅利叶顾捷:机器人行业没包袱,很多事都能从零开始
  • 官方通报汕头违建豪宅“英之园”将强拆:对有关人员严肃追责问责
  • 广西壮族自治区政府主席蓝天立任上被查
  • 特朗普称即将与伊朗达成核协议,外交部:中方愿继续发挥建设性作用
  • 一周文化讲座|“我的生命不过是温柔的疯狂”
  • 透视社会组织创新实践中的花开岭现象:与乡村发展的融合共进