首页 > 其他分享 >vue3 | readonly、shallowReadonly

vue3 | readonly、shallowReadonly

时间:2023-02-07 20:35:29浏览次数:38  
标签:shallowReadonly readonlyObj obj res readonly vue3 const

readonly

接受一个对象(不管是响应式还是普通的)或者是一个ref,返回的是一个原值的只读代理。


<template>
  <n-el class="h-400 w-full flex flex-col justify-center items-center">
    <n-el>{{ data }}</n-el>
    <n-el>{{ readonlyData }}</n-el>
    <n-button @click="handleAddData">增加data</n-button>
    <n-button class="!mt-2" @click="handleAddReadonlyData">增加readonlyData</n-button>
  </n-el>
  </template>
  
  <script setup lang="ts">
  import { readonly, ref } from 'vue'
  const data = ref(100)
  const readonlyData = readonly(data)

  const handleAddData = () => {
    data.value++
  }

  const handleAddReadonlyData = () => {
    readonlyData.value++
  }
  </script>

编辑器提示

image.png

动画.gif

shallowReadonly

readonly()的浅层作用形式

<template>
 <n-el class="h-400 w-full flex flex-col justify-center items-center">
    <n-el>{{ obj.a }}</n-el>
    <n-el>{{ obj.b.res }}</n-el>
    <n-el>{{ obj.b.c.res }}</n-el>
    <n-el>------------------------</n-el>
    <n-el>{{ readonlyObj.a }}</n-el>
    <n-el>{{ readonlyObj.b.res }}</n-el>
    <n-el>{{ readonlyObj.b.c.res }}</n-el>
    <n-button class="!mt-2" @click="handleAddObj">增加obj</n-button>
    <n-button class="!mt-2" @click="handleAddReadonlyObj">增加readonlyObj</n-button>
  </n-el>
 </template>
 
  <script setup lang="ts">
  import { reactive, shallowReadonly } from 'vue'
    const obj = reactive({
    a: 1,
    b: {
      res: 2,
      c: {
        res: 3,
      },
    },
  })

  const readonlyObj = shallowReadonly(obj)

  const handleAddObj = () => {
    obj.a = 100
    obj.b.res = 200
    obj.b.c.res = 300
  }

  const handleAddReadonlyObj = () => {
    readonlyObj.a = 100
    readonlyObj.b.res = 200
    readonlyObj.b.c.res = 300
  }
</script>

image.png

增加obj

动画.gif

增加readonlyObj

动画.gif

标签:shallowReadonly,readonlyObj,obj,res,readonly,vue3,const
From: https://www.cnblogs.com/yangyukeke/p/17099705.html

相关文章

  • vue3 | isRef、unref、toRef、toRefs
    isRef检查某个值是否是ref。是返回true,否则返回false。constnum=ref(10);constnum1=20;constnum2=reactive({data:30});console.log(isRef(num));//tru......
  • vue3 | slots
    一、什么是插槽插槽就是子组件中的提供给父组件使用的一个占位符,用<slot></slot> 表示,父组件可以在这个占位符中填充任何模板代码,如HTML、组件等,填充的内容会替换子组......
  • vue3 | shallowReactive 、shallowRef、triggerRef
    shallowReactive使用reactive声明的变量为递归监听,使用shallowReactive声明的变量为非递归监听(通俗的讲就是reactive创建的对象将会被vue内部进行递归监听,可以监......
  • Vite+Vue3+TS 自定义全局组件,无法不能高亮解析的解决办法
    检查package.json中的devDependencies是否安装@vue/runtime-core依赖,没有的话,安装后重启VSCode一、安装依赖$yarnadd@vue/runtime-core-D$yarnaddunplugin-vue-......
  • vue3+ts基础使用
    详见:vue官方文档响应式数据<scriptsetuplang="ts">import{ref,reactive,computed}from"vue";importtype{Ref}from"vue";//ref//可通过Ref或......
  • vue3新增特性合集
    封装公共方法到原型上再vue2中全局挂载方法使用的是Vue.prototype.xxx的形式来挂载的,但是在vue3中这个方法将不能使用,取而代之的是app.config.globalProperties......
  • vue3 中好用的插件
    1.Api自动导入unplugin-auto-import自动引入compositionapi,不需要再手动引入。(npm地址)下载npmi-Dunplugin-auto-import配置vite.config.tsimportAutoIm......
  • vue3.0 同一项目中调用多个域名的请求
    1.简单粗暴形式:复制多个request.js文件,设置不同的baseUrl,根据需要引用不同的request.js文件。可以解决问题,但不推荐使用2.参数配置形式:利用参数配置,可灵活的调用多个不......
  • vue3 手写dropdown
    <template><divclass="drp_component":class="classname"><p@click="openDrp"class="ws-n"ref="drpDef"title=""><slot></slot></p></div......
  • 面试官:vue2和vue3的区别有哪些?
    一、Vue3与Vue2区别详述1.生命周期对于生命周期来说,整体上变化不大,只是大部分生命周期钩子名称上+“on”,功能上是类似的。不过有一点需要注意,Vue3在组合式API(Comp......