App.vue 924 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <template>
  2. <h2>readonly 和 shallowReadonly</h2>
  3. <h3>state: {{ state }}</h3>
  4. <button @click="update">更新</button>
  5. </template>
  6. <script lang="ts">
  7. import { defineComponent, reactive, readonly, shallowReactive, shallowReadonly } from 'vue'
  8. export default defineComponent({
  9. //readonly 只读属性
  10. //shallowReadonly 浅只读属性
  11. //使其自身的property对象 为只读
  12. //但是不执行嵌套对象的深度只读转换
  13. setup () {
  14. const state = reactive({
  15. name: 'zs',
  16. friend: {
  17. name: 'lisi'
  18. }
  19. })
  20. const state2 = readonly(state)
  21. const state3 = shallowReadonly(state)
  22. const update = ()=>{
  23. // state2.name += '=' //报错 只读属性
  24. // state2.friend.name += '=' //报错
  25. // state3.name += '=' //报错 只读属性
  26. state3.friend.name += '!'
  27. }
  28. return {
  29. state,
  30. update
  31. }
  32. }
  33. })
  34. </script>
  35. <style scoped>
  36. </style>