小程序开发|小程序制作|小程序开发网

搜索

vue3+ts 中 ref与reactive 如何指定类型

2022-12-23 14:08| 发布者: 蠡湖冲| 查看: 558| 评论: 0

摘要: ref 的基础特性ref 约等于 reactive({ value: x })ref() 可以定义时无参数,第一次赋值任意类型,然后就不能增加属性const refa = ref(6)const rcta = reactive({ value: 12 })console.log('refa:', refa) //RefImpl

ref 的基础特性

ref 约等于 reactive({ value: x })
ref() 可以定义时无参数,第一次赋值任意类型,然后就不能增加属性

const refa = ref(6)const rcta = reactive({ value: 12 })console.log('refa:', refa) //RefImpl{...}console.log('refa:', refa.value) //6console.log('rcta:', rcta) //Proxy {value: 12}console.log('rcta.value:', rcta.value) //12const refb = ref({ name: 'bbb' })console.log('refb:', refb.value, refb.value.name) //Proxy{name: 'bbb'}   bbb//refb.value.age=18 //报错 //类型{ name: string;}上不存在属性age

如何在ref中指定类型

const a = ref('') //根据输入参数推导字符串类型 Refconst b = ref([]) //可以通过范型显示约束 Refconst c: Ref = ref([]) //声明类型 Ref const list = ref([1, 3, 5])console.log('list前:', list.value)list.value[1] = 7console.log('list后:', list.value)type typPeople = {  name: string  age: number}const list2: Ref = ref([])console.log('list2-前:', list2.value) //{} 不是空数组,而是空对象list2.value.push({ name: '小张', age: 18 })console.log('list2-后:', list2.value[0]) //{name: '小张', age: 18}********* ref 内部值指定类型 *********const foo = ref('foo')foo.value = 123********* 如果ref类型未知,则建议将 ref 转换为 Ref: *********function useState(initial: T) {  const state = ref(initial) as Ref  return state}const item: typPeople = { name: 'aa', age: 18 }const x1 = useState(item) // x1 类型为: Refconst x2 = ref(item) //x2 类型为: Ref<{ name:string; age: number;}>console.log('ref.value:', x1.value, x1.value.name) //Proxy{name: 'aa', age: 18}  aa

reactive

返回对象的响应式副本
reactive(x) 必须要指定参数,所以类型就已经确定了,也不能增加属性

const count = ref(1)console.log('ref:', count) //RefImpl{...}//当ref分配给reactive时,ref将自动解包const obj = reactive({ a: count }) //不需要count.valueconsole.log(obj.a) // 1console.log(obj.a === count.value) // true//obj.b=7 //添加属性会报错 // { a: number; }上不存在属性b//const str=reactive("aaa")   //这是报错的,reactive参数只能是对象const arr = reactive([1, 2]) //数组,其实结果还是对象const obj = reactive({ 0: 1, 1: 2 })console.log('arr', arr) //Proxy {0: 1, 1: 2}console.log('obj', obj) //Proxy {0: 1, 1: 2}//reactive定义和ref不同,ref返回的是Ref类型,reactive不存在Reactive//它返回是UnwrapNestedRefs,和传入目标类型一致,所以不存在定义通用reactive类型function reactiveFun(target: T) {  const state = reactive(target) as UnwrapNestedRefs  return state}type typPeople = {  name: string  age: number}const item: typPeople = { name: 'aa', age: 18 }const obj1 = reactive(item) //obj1 类型为: { name: string; age: number; }const obj2 = reactiveFun(item) //obj2 类型为: { name: string; age: number; }

isRef、isReactive

// isRef 检查值是否为一个 ref 对象console.log('是否为ref:', isRef(count)) //true//isProxy 检查对象是否是 由reactive或readonly创建的 proxy//readonly是原始对象的只读代理console.log('ref是否为proxy:', isProxy(count)) //falseconsole.log('reactive是否为proxy:', isProxy(obj)) //true//isReactive 检查对象是否是由 reactive 创建的响应式代理console.log('isReactive判断:', isReactive(obj)) //true

toRef、toRefs、toRaw

toRef 为源响应式对象上的某个元素 新创建一个 ref
toRefs 将响应式对象Proxy 转换为普通对象,且元素都指向原始对象的ref
toRaw 返回 reactive或readonly代理的原始对象

toRef 当你要将 prop 的 ref 传递给复合函数时,toRef 很有用

const state = reactive({  foo: 1,  bar: 2})console.log(state.foo) //1state.foo++console.log(state.foo) //2const fooRef = toRef(state, 'foo')fooRef.value++console.log(state.foo) //3  但state.foo并没有.value属性,不要混淆

toRefs 将响应式对象Proxy转换为普通对象,且元素都指向原始对象的ref
toRaw 返回 reactive或readonly 代理的原始对象,当然也可以返回ref的原始对象

const state = reactive({  foo: 1,  bar: 2})console.log(state) //Proxy {foo: 1, bar: 2}const refs1 = toRefs(state) //toRefs 将响应式对象Proxy 转换为普通对象console.log('toRefs:', refs1) //{foo: ObjectRefImpl, bar: ObjectRefImpl}const refs2 = toRaw(state) //toRaw 返回 reactive或readonly代理的原始对象console.log('toRaw:', refs2) //{foo: 1, bar: 2}const ref1 = ref(5) //ref并不是Proxy,而是RefImplconst refs3 = toRefs(ref1) //不报错,但没意义

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

鲜花

握手

雷人

路过

鸡蛋

最新评论

返回顶部