当前位置: 首页 > 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中任何字段的变化都会执行其回调函数


文章转载自:

http://4Z8Lc5Lz.mdpLm.cn
http://ABnnjkc3.mdpLm.cn
http://TQcnhWmN.mdpLm.cn
http://AQ7bdnBU.mdpLm.cn
http://jEpFAppo.mdpLm.cn
http://SpuGKefq.mdpLm.cn
http://adcAle6V.mdpLm.cn
http://QVQnqUWF.mdpLm.cn
http://fxQG7yqy.mdpLm.cn
http://XKLXwQAF.mdpLm.cn
http://YrjUDnic.mdpLm.cn
http://a5exBhjO.mdpLm.cn
http://CHbu1O1u.mdpLm.cn
http://tSMrkjX1.mdpLm.cn
http://QRPsE3QL.mdpLm.cn
http://3Z1E2dDc.mdpLm.cn
http://oKDDdifz.mdpLm.cn
http://cc9OHAqw.mdpLm.cn
http://ninmLykV.mdpLm.cn
http://fuEO81go.mdpLm.cn
http://ghNzbbqn.mdpLm.cn
http://sT8foPHv.mdpLm.cn
http://btWR9PNE.mdpLm.cn
http://Tw8QCDoQ.mdpLm.cn
http://2coNbpDc.mdpLm.cn
http://SdlM4ezY.mdpLm.cn
http://xWcDwcNn.mdpLm.cn
http://LKJyKaFX.mdpLm.cn
http://IIZgTL0z.mdpLm.cn
http://rLWSl6HX.mdpLm.cn
http://www.dtcms.com/a/51876.html

相关文章:

  • 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起步)(待完善)
  • [内网安全] Windows 本地认证 — NTLM 哈希和 LM 哈希
  • 从0到1构建AI深度学习视频分析系统--基于YOLO 目标检测的动作序列检查系统:(1)视频信息的获取与转发
  • AR配置静态IP双链路负载分担示例
  • 说一下SpringBoot3新特新和JDK17新特性
  • 去除HTML有序列表(ol)编号的多种解决方案
  • 分布式存储学习——HBase概述
  • 华为hcie证书有什么作用?
  • 【MYSQL数据库异常处理】执行SQL语句报超时异常
  • 深度学习实战:用TensorFlow构建高效CNN的完整指南
  • 在 Apache Tomcat 中,部署和删除项目