vue+ts 第八章(认识watchEffect高级侦听器)

watchEffect

立即执行传入的一个函数,同时响应式追踪其依赖,并在其依赖变更时重新运行该函数。

如果用到message 就只会监听message 就是用到几个监听几个 而且是非惰性 会默认调用一次

let message = ref<string>('')

let message2 = ref<string>('')

watchEffect(() => {

//console.log('message', message.value);

console.log('message2', message2.value);

})

清除副作用

就是在触发监听之前会调用一个函数可以处理你的逻辑例如防抖

import { watchEffect, ref } from 'vue'

let message = ref<string>('')

let message2 = ref<string>('')

watchEffect((oninvalidate) => {

//console.log('message', message.value);

oninvalidate(()=>{

})

console.log('message2', message2.value);

})

停止跟踪 watchEffect 返回一个函数 调用之后将停止更新

const stop = watchEffect((oninvalidate) => {

//console.log('message', message.value);

oninvalidate(()=>{

})

console.log('message2', message2.value);

},{

flush:"post",

onTrigger () {

}

})

stop()

更多的配置项

副作用刷新时机 flush 一般使用post
pre sync post
更新时机 组件更新前执行 强制效果始终同步触发 组件更新后执行

onTrigger 可以帮助我们调试 watchEffect

import { watchEffect, ref } from 'vue'
let message = ref<string>('')
let message2 = ref<string>('')
watchEffect((oninvalidate) => {
//console.log('message', message.value);
oninvalidate(()=>{
})
console.log('message2', message2.value);
},{
flush:"post",
onTrigger () {
}
})

相关推荐
打小就很皮...21 分钟前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡1 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜052 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx2 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9993 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
后海 0_o3 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构
Scabbards_3 小时前
CPT304-2425-S2-Software Engineering II
前端
小满zs3 小时前
Zustand 第二章(状态处理)
前端·react.js
程序猿小D3 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
萌萌哒草头将军3 小时前
🚀🚀🚀Prisma 发布无 Rust 引擎预览版,安装和使用更轻量;支持任何 ORM 连接引擎;支持自动备份...
前端·javascript·vue.js