shallowReadonly.vue 782 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <template>
  2. <div>
  3. <h1>shallowReadonly</h1>
  4. <h3>
  5. {{ obj.name }} -- {{ obj.age }}
  6. </h3>
  7. <h3>
  8. {{ obj1.name }} -- {{ obj1.age }}--{{ obj1.address.city }}
  9. </h3>
  10. <button @click="changeMain">修改</button>
  11. </div>
  12. </template>
  13. <script lang="ts" setup>
  14. import {shallowReadonly,reactive} from "vue"
  15. let obj = reactive({
  16. name: '张三',
  17. age: 18,
  18. address:{
  19. city:"北京"
  20. }
  21. })
  22. let obj1 = shallowReadonly(obj);
  23. const changeMain = () => {
  24. // Object.assign(obj1,{
  25. // name: '李四',
  26. // age: 20
  27. // })
  28. // obj1.name = '李四'
  29. // obj1.address = {
  30. // city:"天津"
  31. // }
  32. obj1.address.city = '上海'
  33. }
  34. console.log(obj);
  35. console.log(obj1)
  36. </script>
  37. <style lang="scss" scoped>
  38. </style>