当前位置:网站首页>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>
边栏推荐
- Network foundation - header, encapsulation and unpacking
- 品牌·咨询标准化
- Paranoid unqualified company
- Sqlserver multithreaded query problem
- Multidisciplinary integration
- How to do sports training in venues?
- Anr principle and Practice
- SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
- $parent(获取父组件) 和 $root(获取根组件)
- How Oracle backs up indexes
猜你喜欢
sql中对集合进行非空校验
Academic report series (VI) - autonomous driving on the journey to full autonomy
How does an enterprise manage data? Share the experience summary of four aspects of data governance
. Net core accesses uncommon static file types (MIME types)
Non empty verification of collection in SQL
MOS管参数μCox得到的一种方法
华为机试题素数伴侣
How DHCP router works
Paranoid unqualified company
Config distributed configuration center
随机推荐
Tujia, muniao, meituan... Home stay summer war will start
IP address
Asynchronous components and suspend (in real development)
main函数在import语句中的特殊行为
$parent(获取父组件) 和 $root(获取根组件)
Please ask a question, flick Oracle CDC, read a table without update operation, and repeatedly read the full amount of data every ten seconds
JDBC database connection pool usage problem
Select the product attribute pop-up box to pop up the animation effect from the bottom
A slow SQL drags the whole system down
Brand · consultation standardization
Basic process of network transmission using tcp/ip four layer model
leetcode 509. Fibonacci number
Libcurl returns curlcode description
【JDBC以及内部类的讲解】
The latest trends of data asset management and data security at home and abroad
Composition API 前提
栈题目:有效括号的嵌套深度
[noi simulation] regional division (conclusion, structure)
Sword finger offer high quality code
MATLAB小技巧(30)非线性拟合 lsqcurefit