Demo.vue 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <h2>当前求和为:{{sum}}</h2>
  3. <button @click="sum++">点我+1</button>
  4. <hr>
  5. <h2>当前的信息为:{{msg}}</h2>
  6. <button @click="msg+='!'">修改信息</button>
  7. <hr>
  8. <h2>姓名:{{person.name}}</h2>
  9. <h2>年龄:{{person.age}}</h2>
  10. <h2>薪资:{{person.job.j1.salary}}K</h2>
  11. <button @click="person.name+='~'">修改姓名</button>
  12. <button @click="person.age++">增长年龄</button>
  13. <button @click="person.job.j1.salary++">涨薪</button>
  14. </template>
  15. <script>
  16. import {ref,reactive,watch} from 'vue'
  17. export default {
  18. name: 'Demo',
  19. setup(){
  20. //数据
  21. let sum = ref(0)
  22. let msg = ref('你好啊')
  23. let person = ref({
  24. name:'张三',
  25. age:18,
  26. job:{
  27. j1:{
  28. salary:20
  29. }
  30. }
  31. })
  32. console.log(person)
  33. watch(sum,(newValue,oldValue)=>{
  34. console.log('sum的值变化了',newValue,oldValue)
  35. })
  36. watch(person,(newValue,oldValue)=>{
  37. console.log('person的值变化了',newValue,oldValue)
  38. },{deep:true})
  39. //返回一个对象(常用)
  40. return {
  41. sum,
  42. msg,
  43. person
  44. }
  45. }
  46. }
  47. </script>