Vue3的to系列的内置方法(toRef/toRefs/toRaw)

1、toRef

将对象中的单个属性变成响应式的。

//如下值变了,但是视图是不会变化的
import {toRef} from "vue"
const obj = {
  name:"xiaolong",
  height:"178",
  count:2
}
const state =  toRef(obj,'count')

const change = ()=>{
	state.value++
	console.log("+++++++++++++++原始对象",obj)
	console.log("+++++++++++++++引用对象",state)
}

//如果原始对象是个代理对象是会变化,也就是之前的对象是响应式的话是会变化的,如下视图是会变化的
import {toRef,reactive} from "vue"
const obj = reactive({
  name:"xiaolong",
  height:"178",
  count:2
})
const state =  toRef(obj,'count')

const change = ()=>{
	state.value++
	console.log("+++++++++++++++原始对象",obj)
	console.log("+++++++++++++++引用对象",state)
}

2、toRefs

import {toRefs,reactive} from "vue"
let {name,height,count} = reactive({
  name:"xiaolong",
  height:"178",
  count:2
})
console.log(name,height,count)  //这样打印这三个就是个基本数据类型的

//这样打印的话就是响应类型的

import {toRefs,reactive} from "vue"
let obj = reactive({
  name:"xiaolong",
  height:"178",
  count:2
})
let {name,height,count} = toRefs(obj)
console.log(name,height,count)  //这样打印这三个变成响应式的

const change = ()=>{
	count.value++
}

3、toRaw

import {toRaw,reactive} from "vue"
let obj = reactive({
  name:"xiaolong",
  height:"178",
  count:2
})

const rawType= toRaw(obj)
console.log("响应式",obj)//通过proxy代理的
console.log("普通对象,rawType )



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值