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

vue封装useWatch hook支持停止监听和重启监听功能

import { watch, reactive } from 'vue';export function useWatch(source, cb, options) {const state = reactive({stop: null});function start() {state.stop = watch(source, cb, options);}function stop() {state.stop();state.stop = null;}// 返回一个对象,包含start和stop方法return {start,stop};
}

使用:

<template><div><p>Message: {{ message }}</p><button @click="changeMessage">Change Message</button><button @click="stopWatching">Stop Watching</button><button @click="startWatching">Start Watching</button></div>
</template><script>
import { reactive } from 'vue';
import { useWatch } from './useWatch';export default {setup() {const state = reactive({message: 'Hello World',watcher: null});// 使用自定义的useWatch Hook来监听message属性state.watcher = useWatch(() => state.message,(newValue, oldValue) => {console.log('Message changed:', newValue, oldValue);});function changeMessage() {state.message = 'Hello Vue 3';}function stopWatching() {state.watcher.stop();}function startWatching() {state.watcher.start();}return {message: state.message,changeMessage,stopWatching,startWatching};}
};
</script>

http://www.lryc.cn/news/228287.html

相关文章:

  • 智能配方颗粒管理系统解决方案,专业实现中医药产业数字化-亿发
  • PXI总线测试模块-6951E 信号分析仪
  • 精确杂草控制植物检测模型的改进推广
  • C++:对象成员方法的使用
  • 深入了解SpringMvc接收数据
  • 华东“启明”青少年音乐艺术实践中心揭幕暨中国“启明”巴洛克合奏团首演音乐会
  • 17. 机器学习——SVM
  • 算法导论笔记5:贪心算法
  • Vue的高级表格组件库【vxe-table】
  • 从0到0.01入门React | 002.精选 React 面试题
  • 假冒 Skype 应用程序网络钓鱼分析
  • 软件外包开发的需求表达方法
  • 详解JS的四种异步解决方案:回调函数、Promise、Generator、async/await
  • Python进行多线程爬取数据通用模板
  • 基于springboot实现沁园健身房预约管理系统【项目源码】
  • 论文笔记:Deep Trajectory Recovery with Fine-Grained Calibration using Kalman Filter
  • ubuntu下tensorrt环境配置
  • 网络安全基础之php开发文件下载的实现
  • 【学习笔记】 - GIT的基本操作,IDEA接入GIT以及上传hub
  • Antd React Form.Item内部是自定义组件怎么自定义返回值
  • 2023最新ACL大模型论文分类汇总(有代码的)
  • Java版 招投标系统简介 招投标系统源码 java招投标系统 招投标系统功能设计
  • Ubuntu 22.04源码安装cmake 3.27.7
  • 无人地磅称重系统|自助过磅 料仓联动 自助卸料
  • 冥想第九百七十三天
  • ROS 学习应用篇(三)话题Topic学习之自定义话题消息的类型的定义与调用
  • 财税服务展示预约小程序的作用是什么
  • RT-Thread提供的网络世界入口 -net组件
  • 分享一些有趣的MATLAB提示音(代码可直接复制)
  • 软件测试|selenium执行js脚本