在 Vue3 组合 API 中使观察者立即工作

use*_*631 3 vue.js vuejs3 vue-composition-api

使用 Vue3 组合 API。我如何让手表立即工作。以下代码不起作用。

watch((immediate=true) => props.isOpen, () => {
        if (props.isOpen && props.preventBackgroundScrolling) {
          document.body.style.setProperty('overflow', 'hidden')
        } else {
          document.body.style.removeProperty('overflow')
        }

          });
Run Code Online (Sandbox Code Playgroud)

Bou*_*him 7

它应该作为选项放置:

watch(() => props.isOpen, () => {
        if (props.isOpen && props.preventBackgroundScrolling) {
          document.body.style.setProperty('overflow', 'hidden')
        } else {
          document.body.style.removeProperty('overflow')
        }

          },{immediate:true});

Run Code Online (Sandbox Code Playgroud)

或者

watch('props.isOpen', () => {
        if (props.isOpen && props.preventBackgroundScrolling) {
          document.body.style.setProperty('overflow', 'hidden')
        } else {
          document.body.style.removeProperty('overflow')
        }

          },
       {immediate:true}
   );

Run Code Online (Sandbox Code Playgroud)