JavaScript/vue.js/watch:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第16行: 第16行:
<syntaxhighlight lang="html">
<syntaxhighlight lang="html">
<script>
<script>
import { getCurrentInstance } from 'vue'
export default defineComponent({
export default defineComponent({
   // ...
   // ...
第24行: 第25行:
   },
   },
   setup(props, vm) {
   setup(props, vm) {
    const { proxy } = getCurrentInstance()
     // ...
     // ...
     vm.$watch("count")();
     proxy.$watch("foo")();
   }
   }
   // ...
   // ...
})
})
</script></syntaxhighlight>
</script></syntaxhighlight>

2025年3月18日 (二) 13:53的版本

VUE 3

多参数监听

<script setup>
import { ref } from 'vue'
watch(
  () => [foo, bar],
  (_new, _old) => {
    // ...
  }
)
</script>

停止监听

<script>
import { getCurrentInstance } from 'vue'
export default defineComponent({
  // ...
  watch: {
    foo(_new, _old) {
      // ...
    }
  },
  setup(props, vm) {
    const { proxy } = getCurrentInstance()
    // ...
    proxy.$watch("foo")();
  }
  // ...
})
</script>