当前位置:网站首页>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>
边栏推荐
- 学习突围2 - 关于高效学习的方法
- 记一次 .NET 某新能源系统 线程疯涨 分析
- Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
- ESP32构解工程添加组件
- PACP学习笔记一:使用 PCAP 编程
- Vscade editor esp32 header file wavy line does not jump completely solved
- leecode3. 无重复字符的最长子串
- The difference between cache and buffer
- Esp32 series column
- Test next summary
猜你喜欢
随机推荐
MongoDB命令汇总
About how appium closes apps (resolved)
Unity build error: the name "editorutility" does not exist in the current context
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
Some principles of mongodb optimization
Read PG in data warehouse in one article_ stat
LIS longest ascending subsequence problem (dynamic programming, greed + dichotomy)
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
MATLAB中polarscatter函数使用
JS缓动动画原理教学(超细节)
[Presto profile series] timeline use
Mongodb replication (replica set) summary
JS function 返回多个值
Cinnamon Applet 入门
学习突围2 - 关于高效学习的方法
Talk about pseudo sharing
Cmake learning and use notes (1)
Realbasicvsr test pictures and videos
高端了8年,雅迪如今怎么样?
国泰君安证券开户怎么开的?开户安全吗?