ref和reactive区别

ref是什么类型变量都可以定义,而reactive只能定义数组和对象

比如

ref定义字符串

//定义
const msg=ref("hellow world");//
//事件调用
function append(){//拼感叹号在尾巴
	msg.value+="!"
}

ref定义对象

//定义
const counter=ref({num:1})
//事件调用
function append(){//拼感叹号在尾巴
	msg.value.num+="!"
}

reactive只能定义对象和数组

reactive定义对象

//定义
const counter=reactive({num:1})
//事件调用
function append(){//拼感叹号在尾巴
	msg.num+="!"
}

reactive定义数组

const counter=reactive([{id:1,name:"你好"}])

function pusher(){
	counter.push({id:2,name:"啊啊"})
}

你对比下上面两个定义对象时候的写法

ref 是都要写.value的 所以看个人习惯,博主闲麻烦。

注意   ,你最好跑跑博主的ref例子,能明白多少看造化了 

<script setup lang="ts">
	import {ref } from 'vue'
    interface myType{
		id:Number;
		name?:string
	}
	//type myType={
	//	id:Number,
	//	name?:string
	//}
	const obj={
		a:ref<string|number>(0),//这样写,string|number  就算值突然变成 字符串,他也是响应的
		b:ref<myType[]|null>(null),
		c:"dsad",//不响应
	}
	let {a,b,c}=obj;
	a.value="sss"
	b.value=[{id:1},{id:2,name:"你好"},{id:3,name:"啊啊"}]
	c="eeeee"
</script>
<template>
	<div class="box">
		<h3></h3>
		<div>
			{{obj}}
			<br/>
			a是响应的 {{a}}	
			<br/>
			b是响应式{{b}}
			<br/>
			c不响应   {{c}}
			<br/>
			d不响应 {{d}}
		</div>
	</div>
	
</template>
<style>
	.box{display: block;text-align:center;margin: 50px 0 0;}
	.ml_10{margin-left:10px}
</style>

下面就是 reactive+ts了。如果有错误可以在评论区指出。谢谢

<script setup lang="ts">
	import {reactive,ref } from 'vue'
    interface myType{
		id:Number;
		name?:string
	}
    
	//type myType={
	//	id:Number,
	//	name?:string
	//}
	// const obj=reactive({
	// 	a:1,
	// 	b:ref<null|myType[]>(null)
	// })
	//也可以写成下面,先申明在 赋值
	const obj:{
		a:number|string;
		b:null|myType[]
	}=reactive({
		a:1,
		b:null
	})
	obj.b=[{id:1,name:"你好"}]
	obj.a=2// 绑定
</script>
<template>
	<div class="box">
		<h3></h3>
		<div>
			{{obj}}
			<br/>
			a 值是{{obj.a}}  绑定
			<br/>
			b 值是{{obj.b}}  绑定
			<br/>
		</div>
	</div>
	
</template>
<style>
	.box{display: block;text-align:center;margin: 50px 0 0;}
	.ml_10{margin-left:10px}
</style>

关于refs组件的绑定

<script setup lang="ts">
	import {onMounted, ref } from 'vue'
	const el=ref<HTMLInputElement|null>(null)
	const inputVal=ref<string>("")
	const inputVal2=ref("")
	onMounted(()=>{
		el.value?.focus();
	})
	// const inputEvent=(e:any)=>{
	// 	inputVal.value=e.target.value
	// }
	//写法二
	const inputEvent=(e:Event)=>{
		inputVal.value=(e.target as HTMLInputElement).value
	}
</script>
<template>
	<div class="box">
		<h3></h3>
		<div>
			inputVal:{{inputVal}}<br/>
			inputVal2:{{inputVal2}}<br/>
			<input type="text" @change="inputEvent" ref="el" v-model='inputVal2'/>
		</div>
	</div>
	
</template>
<style>
	.box{display: block;text-align:center;margin: 50px 0 0;}
	.ml_10{margin-left:10px}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪狼之夜

打个赏,让博主知道博文没白写

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值