Skip to content

Commit

Permalink
doc: Update vue.md (#786)
Browse files Browse the repository at this point in the history
添加 vue3 watch 监听多个值的模板
  • Loading branch information
Mieriki authored Jul 8, 2024
1 parent 8e9132f commit 3cbec70
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,44 @@ watch(count, function() {
```
<!--rehype:className=wrap-text-->

### 监听多个值
<!--rehype:wrap-class=col-span-2-->

```html
<template>
<h1> {{ count1 }} </h1>
<h1> {{ count2 }} </h1>
<button @click="count1++">count1</button>
<button @click="count2++">count2</button>
</template>

<script setup>
import { watch, ref } from 'vue';
const count1 = ref(0)
const count2 = ref(0)
watch(
// 监听的表达式或函数
() => ({
count1: count1.value,
count2: count2.value
}),
// 回调函数
(newValue, oldValue) => {
// 在这里执行需要的逻辑
console.log('count1 或 count2 变化了:', newValue);
},
// immediate: true 表示在初始渲染时立即执行一次回调函数,以便处理初始的状态。
// deep: true 表示深度监听,即对 newValue 和 oldValue 进行深层比较,而不是简单的引用比较。
{ immediate: true, deep: true }
);
</script>

<style scoped>
</style>
```

### 计算状态
<!--rehype:wrap-class=col-span-2-->

Expand Down

0 comments on commit 3cbec70

Please sign in to comment.