vue3中watch函数的坑

main
roger 2 years ago
parent 41cacb3eaf
commit 0efe14f7e8
  1. 13
      20_脚手架/vue3_test/8.src_watch函数/App.vue
  2. BIN
      20_脚手架/vue3_test/8.src_watch函数/assets/logo.png
  3. 87
      20_脚手架/vue3_test/8.src_watch函数/components/demo.vue
  4. 10
      20_脚手架/vue3_test/8.src_watch函数/main.js
  5. 97
      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,87 @@
<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} 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 = reactive({
name: '张三',
age: 18,
job: {
j1: {
salary: 20
}
}
})
// ref
// watch(sum, (newValue, oldValue) => {
// console.log('sum', newValue, oldValue)
// }, {deep: true, immediate: true})
// ref
watch([sum, msg], (newValue, oldValue) => {
console.log('sum和msg的值变化了', newValue, oldValue)
}, {immediate: true})
// reactiveoldValuedeep
// watch(person, (newValue, oldValue) => {
// console.log('person', newValue, oldValue)
// }, {deep: false})
// reactive
// watch(()=>person.age, (newValue,oldValue)=>{
// console.log('person.age', newValue, oldValue)
// })
// reactive
watch([() => person.name, () => person.age], (newValue, oldValue) => {
console.log('person.age和.name对象的值变了', newValue, oldValue)
})
//
watch(() => person.job, (newValue, oldValue) => {
console.log('person.job对象的值变了', newValue, oldValue)
}, {deep: true})
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')

@ -1,49 +1,86 @@
<template>
<h1>个人信息</h1>
<input type="text" v-model="person.firstName">
<br>
<input type="text" v-model="person.lastName">
<br>
<span>全名 {{person.fullName}}</span>
<br>
全名<input type="text" v-model="person.fullName">
<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, computed} from "vue";
import {reactive, ref, watch} from "vue";
export default {
name: 'Demo',
// computed:{
// fullName() {
// return this.person.firstName+'-'+this.person.lastName
// }
// 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 = reactive({
firstName: '张',
lastName: '三'
})
// -
person.fullName = computed(()=>{
return person.firstName + '-' + person.lastName
name: '张三',
age: 18,
job: {
j1: {
salary: 20
}
}
})
// -
person.fullName = computed({
get() {
return person.firstName + '-' + person.lastName
},
set(val) {
const nameArr = val.split('-')
person.firstName = nameArr[0]
person.lastName = nameArr[1]
// ref
// watch(sum, (newValue, oldValue) => {
// console.log('sum', newValue, oldValue)
// }, {deep: true, immediate: true})
}
// ref
watch([sum, msg], (newValue, oldValue) => {
console.log('sum和msg的值变化了', newValue, oldValue)
}, {immediate: true})
// reactiveoldValuedeep
// watch(person, (newValue, oldValue) => {
// console.log('person', newValue, oldValue)
// }, {deep: false})
// reactive
// watch(()=>person.age, (newValue,oldValue)=>{
// console.log('person.age', newValue, oldValue)
// })
// reactive
watch([() => person.name, () => person.age], (newValue, oldValue) => {
console.log('person.age和.name对象的值变了', newValue, oldValue)
})
//
watch(() => person.job, (newValue, oldValue) => {
console.log('person.job对象的值变了', newValue, oldValue)
}, {deep: true})
return {
person,
sum,
msg,
person
}
}
}

Loading…
Cancel
Save