其他分享
首页 > 其他分享> > readonly 和 shallowReadonly

readonly 和 shallowReadonly

作者:互联网

readonly函数

<template>
  <div>当前求和为 {{sum}}</div>
  <div>{{name}} --- {{age}} --- {{job.j1.salary}}</div>
  <div>
    <button @click="name+='~'">改名</button>
    <button @click="age++">增加年龄</button>
    <button @click="job.j1.salary+=10">增加工资</button>
  </div>
</template>

<script>
import {reactive, ref, toRefs, readonly, } from 'vue'
export default {
  setup() {
    let person = reactive({
      name: '张三',
      age: 18,
      job: {
        j1: {
          salary: 3000
        }
      }
    })
    person = readonly(person) // 现在person里面的所有属性都是只读的
    let sum = ref(0)
    return {
      sum,
      ...toRefs(person)
    }
  }
}
</script>

shallowReadonly函数

<template>
  <div>当前求和为 {{sum}}</div>
  <div>{{name}} --- {{age}} --- {{job.j1.salary}}</div>
  <div>
    <button @click="name+='~'">改名</button>
    <button @click="age++">增加年龄</button>
    <button @click="job.j1.salary+=10">增加工资</button>
  </div>
</template>

<script>
import {reactive, ref, toRefs, shallowReadonly} from 'vue'
export default {
  setup() {
    let person = reactive({
      name: '张三',
      age: 18,
      job: {
        j1: {
          salary: 3000
        }
      }
    })
    person = shallowReadonly(person) // 现在person对象里面的第一层属性是只读的
    let sum = ref(0)
    return {
      sum,
      ...toRefs(person)
    }
  }
}
</script>

标签:shallowReadonly,sum,j1,---,person,readonly
来源: https://www.cnblogs.com/xiebenyin-/p/15857541.html