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

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== VUE 3 ==”
 
Kangkk​(留言 | 贡献)
 
(未显示同一用户的4个中间版本)
第1行: 第1行:
== VUE 3 ==
== VUE 3 ==
=== 多参数监听 ===
<syntaxhighlight lang="html">
<script setup>
import { ref } from 'vue'
watch(
  () => [foo, bar],
  (_new, _old) => {
    // ...
  }
)
</script></syntaxhighlight>
=== 停止监听 ===
<syntaxhighlight lang="html">
<script>
import { getCurrentInstance } from 'vue'
export default defineComponent({
  // ...
  watch: {
    foo(_new, _old) {
      // ...
    }
  },
  setup(props, vm) {
    const { proxy } = getCurrentInstance()
    // ...
    proxy.$watch("foo")();
  }
  // ...
})
</script></syntaxhighlight>
=== onTrigger ===
=== watchEffect ===
<syntaxhighlight lang="html5">
<script>
import { watchEffect } from 'vue'
// ...
  setup() {
    watchEffect(() => {
      console.log('count changed to', count.value);
    });
    // ...
</script>
</syntaxhighlight>