You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

<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)
// // }
// // }
2 years ago
// },
2 years ago
setup() {
let sum = ref(0)
let msg = ref('你好呀')
let person = ref({
name: '张三',
age: 18,
job: {
j1: {
salary: 20
}
}
})
// 方法一: ref的对象监视,通过value实现
// watch(person.value, (newValue, oldValue) => {
// console.log('person的值变了', newValue, oldValue)
// })
// 方法二: ref的对象监视,通过deep实现
watch(person, (newValue, oldValue) => {
console.log('person的值变了', newValue, oldValue)
},{deep: true})
return {
sum,
msg,
person
}
}
}
</script>