SoFunction
Updated on 2025-04-13

Implementation of WatchEffect Advanced Listener in vue3

This is a supplement to the knowledge point. I won’t introduce it more and I’m tired. I just upload the code to comment. I’ll read it myself

Basic usage

let message = ref<string>('airplane')
let message2 = ref<string>('cup')
 watchEffect((oninvalidate) => {
  // Listen directly for the first time you load  ('message--', );
  ('message2--', );
})

You can manually pause the monitoring

let message = ref<string>('airplane')
let message2 = ref<string>('cup')
// stop is a function, and you can stop listening by callingconst stop = watchEffect((oninvalidate) => {
  // Listen directly for the first time you load  ('message--', );
  ('message2--', );
  // Oninvalidate clears some side effects and will call this callback function first.  oninvalidate(() => {
    ('before');
  })
})
const stopWatch = () => stop()

Configuration Items

// stop is a function, and you can stop listening by callingconst stop = watchEffect((oninvalidate) => {
  let ipt: HTMLInputElement = ('#ipt') as HTMLInputElement
  ('ipt element', ipt);
  // Oninvalidate clears some side effects and will call this callback function first.  oninvalidate(() => {
    ('before');
  })
}, {
  // The listening is triggered after the component is updated  flush: 'post',
  // Develop environment helps debug  onTrigger(e) {
    ('onTrigger---', e);
  }
})

This is the end of this article about the implementation of WatchEffect Advanced Listener in vue3. For more related contents of vue3 WatchEffect Advanced Listener, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!