vue3中watch监视ref的数据的说明

main
roger 2 years ago
parent 0efe14f7e8
commit 6d55aa3010
  1. 13
      20_脚手架/vue3_test/9.src_watch监视ref的数据的说明/App.vue
  2. BIN
      20_脚手架/vue3_test/9.src_watch监视ref的数据的说明/assets/logo.png
  3. 67
      20_脚手架/vue3_test/9.src_watch监视ref的数据的说明/components/demo.vue
  4. 10
      20_脚手架/vue3_test/9.src_watch监视ref的数据的说明/main.js
  5. 36
      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,67 @@
<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 = ref({
name: '张三',
age: 18,
job: {
j1: {
salary: 20
}
}
})
// refvalue
// watch(person.value, (newValue, oldValue) => {
// console.log('person', newValue, oldValue)
// })
// refdeep
watch(person, (newValue, oldValue) => {
console.log('person的值变了', 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')

@ -37,7 +37,7 @@ export default {
setup() {
let sum = ref(0)
let msg = ref('你好呀')
let person = reactive({
let person = ref({
name: '张三',
age: 18,
job: {
@ -47,35 +47,15 @@ export default {
}
})
// 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)
// refvalue
// watch(person.value, (newValue, oldValue) => {
// console.log('person', 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})
// refdeep
watch(person, (newValue, oldValue) => {
console.log('person的值变了', newValue, oldValue)
},{deep: true})
return {
sum,

Loading…
Cancel
Save