侦听器
https://vue3js.cn/docs/zh/guide/reactivity-computed-watchers.html#watch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 | <template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<h1>{{ count }}</h1>
<h1>{{ double }}</h1>
<h1>{{ greetings }}</h1>
<button @click="increase">👍+1</button><br>
<button @click="updateGreeting">Update Title</button>
</div>
</template>
<script lang="ts">
import { ref, computed, reactive, toRefs, watch } from 'vue';
interface DataProps {
count: number;
double: number;
increase: () => void;
}
export default ({
name: 'App',
setup() {
const data: DataProps = reactive({
count: 0,
increase: () => { data.count++ },
double: computed(() => data.count * 2),
})
const greetings = ref('')
const updateGreeting = () => {
console.log('hhhh')
greetings.value += 'Hello! '
}
watch([greetings, () => data.count], (newValue, oldValue) => {
console.log('old', oldValue)
console.log('new', newValue)
document.title = 'updated' + greetings.value + data.count
})
const refData = toRefs(data)
return {
...refData,
greetings,
updateGreeting,
}
}
});
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
|