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

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的2个中间版本)
第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>
=== onTrigger ===
=== watchEffect ===
<syntaxhighlight lang="html5">
<script>
import { watchEffect } from 'vue'
// ...
  setup() {
    watchEffect(() => {
      console.log('count changed to', count.value);
    });
    // ...
</script>
</syntaxhighlight>

2025年3月18日 (二) 14:10的最新版本

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>

onTrigger

watchEffect

<script>
import { watchEffect } from 'vue'
// ...
  setup() {
    watchEffect(() => {
      console.log('count changed to', count.value);
    });
    // ...
</script>