watchEffect函数

main
roger 2 years ago
parent 6d55aa3010
commit 34e444f68e
  1. 13
      20_脚手架/vue3_test/10.src_watchEffect函数/App.vue
  2. BIN
      20_脚手架/vue3_test/10.src_watchEffect函数/assets/logo.png
  3. 54
      20_脚手架/vue3_test/10.src_watchEffect函数/components/demo.vue
  4. 10
      20_脚手架/vue3_test/10.src_watchEffect函数/main.js
  5. 29
      20_脚手架/vue3_test/src/components/demo.vue

@ -0,0 +1,13 @@
<template>
<Demo/>
</template>
<script>
import {reactive} from "vue";
import Demo from "@/components/demo.vue";
export default {
name: 'App',
components: {Demo},
}
</script>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

@ -0,0 +1,54 @@
<template>
<h2>当前求和为{{ sum }}</h2>
<button @click="sum++">点击+1</button>
<hr>
<h2>当前信息为{{ msg }}</h2>
<button @click="msg+='!'">修改信息</button>
<hr>
<h2>姓名{{ person.name }}</h2>
<h2>年龄{{ person.age }}</h2>
<h2>薪资{{ person.job.j1.salary }}</h2>
<button @click="person.name+='~'">修改姓名</button>
<button @click="person.age++">修改年龄</button>
<button @click="person.job.j1.salary++">修改薪资</button>
<h2></h2>
</template>
<script>
import {reactive, ref, watch, watchEffect} from "vue";
export default {
name: 'Demo',
setup() {
let sum = ref(0)
let msg = ref('你好呀')
let person = reactive({
name: '张三',
age: 18,
job: {
j1: {
salary: 20
}
}
})
// refvalue
// watch(person, (newValue, oldValue) => {
// console.log('person', newValue, oldValue)
// })
watchEffect(() => {
const x1 = sum.value
const x2 = person.job.j1.salary
console.log('watchEffect所指定的回调执行了')
})
return {
sum,
msg,
person
}
}
}
</script>

@ -0,0 +1,10 @@
// 引入的不再是Vue构造函数, 引入的是一个名为createApp的工厂函数
import { createApp } from 'vue'
import App from './App.vue'
// createApp(App).mount('#app')
// 创建应用实例对象
const app = createApp(App)
console.log('@@@',app)
app.mount('#app')

@ -15,29 +15,15 @@
</template>
<script>
import {reactive, ref, watch} from "vue";
import {reactive, ref, watch, watchEffect} from "vue";
export default {
name: 'Demo',
// watch: {
// // Vue2
// // sum(newValue, oldValue) {
// // console.log('sum', newValue, oldValue)
// // }
// // Vue2
// // sum: {
// // immediate: true,
// // deep: true,
// // handler(newValue, oldValue) {
// // console.log('sum', newValue, oldValue)
// // }
// // }
// },
setup() {
let sum = ref(0)
let msg = ref('你好呀')
let person = ref({
let person = reactive({
name: '张三',
age: 18,
job: {
@ -48,14 +34,15 @@ export default {
})
// refvalue
// watch(person.value, (newValue, oldValue) => {
// watch(person, (newValue, oldValue) => {
// console.log('person', newValue, oldValue)
// })
// refdeep
watch(person, (newValue, oldValue) => {
console.log('person的值变了', newValue, oldValue)
},{deep: true})
watchEffect(() => {
const x1 = sum.value
const x2 = person.job.j1.salary
console.log('watchEffect所指定的回调执行了')
})
return {
sum,

Loading…
Cancel
Save