当前位置:网站首页>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>
边栏推荐
- Basic introduction of JWT
- .net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context
- Prime partner of Huawei machine test questions
- MATLAB小技巧(30)非线性拟合 lsqcurefit
- AVL树的实现
- 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
- Anr principle and Practice
- linux系统rpm方式安装的mysql启动失败
- From zero to one, I will teach you to build the "clip search by text" search service (2): 5 minutes to realize the prototype
- Multidisciplinary integration
猜你喜欢
随机推荐
Common function detect_ image/predict
Basic process of network transmission using tcp/ip four layer model
Release notes of JMeter version 5.5
Databinding exception of kotlin
Maze games based on JS
基于JS的迷宫小游戏
CompletableFuture使用详解
【NOI模拟赛】区域划分(结论,构造)
Use of completable future
This article introduces you to the characteristics, purposes and basic function examples of static routing
组件的嵌套和拆分
请教一下,监听pgsql ,怎样可以监听多个schema和table
MATLAB小技巧(30)非线性拟合 lsqcurefit
Please ask a question, flick Oracle CDC, read a table without update operation, and repeatedly read the full amount of data every ten seconds
Détailler le bleu dans les tâches de traduction automatique
js小练习
How to model and simulate the target robot [mathematical / control significance]
Jetpack compose is much more than a UI framework~
from .onnxruntime_pybind11_state import * # noqa ddddocr运行报错
Pass parent component to child component: props