Demo.vue 843 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <template>
  2. <h4>当前求和为:{{sum}}</h4>
  3. <button @click="sum++">点我++</button>
  4. <hr>
  5. <h2>姓名:{{name}}</h2>
  6. <h2>年龄:{{age}}</h2>
  7. <h2>薪资:{{job.j1.salary}}K</h2>
  8. <button @click="name+='~'">修改姓名</button>
  9. <button @click="age++">增长年龄</button>
  10. <button @click="job.j1.salary++">涨薪</button>
  11. </template>
  12. <script>
  13. import {ref,reactive,toRefs,readonly,shallowReadonly} from 'vue'
  14. export default {
  15. name: 'Demo',
  16. setup(){
  17. //数据
  18. let sum = ref(0)
  19. let person = reactive({
  20. name:'张三',
  21. age:18,
  22. job:{
  23. j1:{
  24. salary:20
  25. }
  26. }
  27. })
  28. person = readonly(person)
  29. // person = shallowReadonly(person)
  30. // sum = readonly(sum)
  31. // sum = shallowReadonly(sum)
  32. //返回一个对象(常用)
  33. return {
  34. sum,
  35. ...toRefs(person)
  36. }
  37. }
  38. }
  39. </script>