12345678910111213141516171819202122232425262728293031323334353637383940 |
- <template>
- <h2>readonly 和 shallowReadonly</h2>
- <h3>state: {{ state }}</h3>
- <button @click="update">更新</button>
- </template>
- <script lang="ts">
- import { defineComponent, reactive, readonly, shallowReactive, shallowReadonly } from 'vue'
- export default defineComponent({
- //readonly 只读属性
- //shallowReadonly 浅只读属性
- //使其自身的property对象 为只读
- //但是不执行嵌套对象的深度只读转换
- setup () {
- const state = reactive({
- name: 'zs',
- friend: {
- name: 'lisi'
- }
- })
- const state2 = readonly(state)
- const state3 = shallowReadonly(state)
- const update = ()=>{
- // state2.name += '=' //报错 只读属性
- // state2.friend.name += '=' //报错
- // state3.name += '=' //报错 只读属性
- state3.friend.name += '!'
- }
- return {
- state,
- update
- }
- }
- })
- </script>
- <style scoped>
- </style>
|