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,watchEffect} from 'vue'
  17. export default {
  18. name: 'Demo',
  19. setup(){
  20. //数据
  21. let sum = ref(0)
  22. let msg = ref('你好啊')
  23. let person = reactive({
  24. name:'张三',
  25. age:18,
  26. job:{
  27. j1:{
  28. salary:20
  29. }
  30. }
  31. })
  32. //监视
  33. /* watch(sum,(newValue,oldValue)=>{
  34. console.log('sum的值变化了',newValue,oldValue)
  35. },{immediate:true}) */
  36. watchEffect(()=>{
  37. const x1 = sum.value
  38. const x2 = person.job.j1.salary
  39. console.log('watchEffect所指定的回调执行了')
  40. })
  41. //返回一个对象(常用)
  42. return {
  43. sum,
  44. msg,
  45. person
  46. }
  47. }
  48. }
  49. </script>