当前位置:网站首页>Readonly read only
Readonly read only
2022-07-07 07:10:00 【To be a woman of light】
adopt reactive perhaps ref You can get a responsive object , But in some cases , We pass it on to other places ( Components ) The responsive object of wants to be in another place ( Components ) By using , But it can't be modified , How to prevent this situation at this time ?
- vue3 For us readonly Methods
- readonly Will return the read-only proxy of the native object ( That is, it is still a Proxy, This is a proxy Of set Method hijacked , And it cannot be modified );
for example : In this way, our attributes can be modified
const info={name:"jiang"}
info.name="Zhang"
But in some cases : We don't want others to modify our name attribute
// principle : We can actually use a responsive proxy object for data hijacking
// Come back then infoProxy The return value does not need to be modified by others
let infoProxy= new Proxy(info,{
// For example, to a component , The component wants to get our data , have access to get Method
get(target.key){
return target[key] // Get key Go straight back to
set(target.key.value){warning(" It is not allowed to modify variable values ")} // Use data hijacking , If you want to modify the console warning
}
})
const name=infoProxy.name //jiang
infoProxy.name="Zhang".
Use
<template>
<div id="app">
<button @click="increat"></button>
</div>
</template>
<script>
// 1.vue3 Provided reactive API Be responsive
import {reactive,readonly} from 'vue'
export default {
props:{
message:{
type:String,
required:true
}
},
setup(){
// Common object
const info1={name:"jiang"}
// Set the read-only property
const readonlyInfo=readonly(info1)
// Responsive object
const info2=reactive({name:"jiang"})
const readonlyInfo=readonly(info2)
const increat=()=>{
// At this time, when modifying our corresponding agent, we will report the corresponding warning
readonlyInfo.name="zhang"
}
return {
increat,
}
}
}
</script>
边栏推荐
- toRefs API 与 toRef Api
- Advantages of using net core / why
- Distributed ID solution
- Big coffee gathering | nextarch foundation cloud development meetup is coming
- The latest trends of data asset management and data security at home and abroad
- LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
- RuntimeError: CUDA error: CUBLAS_ STATUS_ ALLOC_ Failed when calling `cublascreate (handle) `problem solving
- Libcurl returns curlcode description
- How does an enterprise manage data? Share the experience summary of four aspects of data governance
- Anr principle and Practice
猜你喜欢
数据资产管理与数据安全国内外最新趋势
华为机试题素数伴侣
After the promotion, sales volume and flow are both. Is it really easy to relax?
联合索引ABC的几种索引利用情况
DHCP路由器工作原理
Basic introduction of JWT
Precise space-time travel flow regulation system - ultra-high precision positioning system based on UWB
Pass child component to parent component
SQLMAP使用教程(四)实战技巧三之绕过防火墙
ANR 原理及实践
随机推荐
Please answer the questions about database data transfer
Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
transform-origin属性详解
多个kubernetes集群如何实现共享同一个存储
Sword finger offer high quality code
readonly 只读
华为机试题素数伴侣
Libcurl returns curlcode description
Communication of components
Several index utilization of joint index ABC
Common function detect_ image/predict
jdbc数据库连接池使用问题
How to model and simulate the target robot [mathematical / control significance]
MYSQL----导入导出&视图&索引&执行计划
异步组件和Suspense(真实开发中)
Distributed ID solution
Jetpack Compose 远不止是一个UI框架这么简单~
MOS管参数μCox得到的一种方法
MATLAB小技巧(30)非线性拟合 lsqcurefit
Networkx drawing and common library function coordinate drawing