当前位置:网站首页>toRaw和markRaw
toRaw和markRaw
2022-07-07 11:29:00 【湛海不过深蓝】
toRaw 将一个reactive生成的响应式对象转为普通对象
markRaw 标记一个对象,使其永远不会再成为响应式对象
vue3中,用ref、reactive定义数据,是为了把普通数据,变成响应式数据
某些时候,需要将响应式数据,再变成普通数据,此时就用toRaw
<script lang="ts" setup>
import {
ref, reactive, toRaw, markRaw } from 'vue'
const person = reactive({
name: '哈哈',
age: 10,
job: {
salary: 20
}
})
console.log(person) // proxy 响应式数据
const p = toRaw(person)
console.log(p) // object 普通数据
const sum = ref(0)
const s = toRaw(sum)
console.log(s) // undefined 因为toRaw只能处理reactive所定义的响应式数据
</script>
setup 在整个工作过程中,只调用一次;
当需要在已经定义好的reactive数据里面在添加一个属性时,这个属性也会变成响应式的,可是如果我添加时就不想让他变成响应式的,用markRaw
<script lang="ts" setup>
import {
ref, reactive, toRaw, markRaw } from 'vue'
const person = reactive({
name: '哈哈',
age: 10,
job: {
salary: 20
}
})
let car = {
name: '汽车', price: 40 }
person.car = markRaw(car) // 此时,person里面添加的car就是普通数据
</script>
边栏推荐
- PAcP learning note 3: pcap method description
- JS中为什么基础数据类型可以调用方法
- Practical example of propeller easydl: automatic scratch recognition of industrial parts
- Esp32 construction engineering add components
- 【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
- Milkdown control icon
- Detr introduction
- [QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
- ESP32系列专栏
猜你喜欢
Ogre入门尝鲜
Ways to improve the performance of raspberry pie
[untitled]
Analysis of DHCP dynamic host setting protocol
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
Milkdown control icon
Esp32 construction engineering add components
Cinnamon Applet 入门
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
xshell连接服务器把密钥登陆改为密码登陆
随机推荐
10 pictures open the door of CPU cache consistency
Mongodb slice summary
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
regular expression
Cinnamon 任务栏网速
MySQL master-slave replication
Write it down once Net a new energy system thread surge analysis
Cinnamon Applet 入门
Pcap learning notes II: pcap4j source code Notes
Cookie and session comparison
RecyclerView的数据刷新
Sed of three swordsmen in text processing
一文读懂数仓中的pg_stat
Signal strength (RSSI) knowledge sorting
[learning notes] agc010
[untitled]
How far can it go to adopt a cow by selling the concept to the market?
[learning notes] segment tree selection
Storage principle inside mongodb
抓细抓实抓好安全生产各项工作 全力确保人民群众生命财产安全