vue3

composition-api

setup()

  1. props
  2. ref
  3. reactive
  4. computed
  5. watch
  6. 生命周期
  7. refs
  8. toRefs

ref

接收一个参数值,返回一个响应式且可改变的ref对象

const num  = ref(0);
const num2 = ref(1)
console.log(ref.value)

reactive

接收一个普通对象,返回该普通对象的响应式原理

const state = reactive({
			num1: 0,
			num2: 1,
			result: 0
})
console.log(state.num1)
const num=> (){
	state.result=state.num1+state.num2
}

computed

计算属性

const result=computed(()=> {
	get: ()=> {
		return 2020-state.age
	},
	set:(val)=> {
		state.age = 2020 - val
	}
	
})

watch

const state = reactive({
	age: 18,
	name: zhangsan
})
watch(()=>state.age,(newVal,oldVal)=>{
	console.log(newVal,oldVal)
})
const change = (val)=> {
	state.age += val;
}
监测ref
const age = ref(18)

watch(age,(newVal,oldVal)=>{
	console.log(newVal,oldVal)
)}
监测两个值
watch([()=>state.age,()=>state.name],
([newAge,newName],[oldAge,oldName])=>{
	console.log(newAge,newNmae)
	console.log(oldAge,oldName)
))

生命周期

beforeCreate -> 使用 setup()
created -> 使用 setup()
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
errorCaptured -> onErrorCaptured

refs

<h2 ref="oH2">Vue3<h2>
<botton>click</botton>

const oH2 = ref(null)
const change = ()=> {
 	oH2.value.style.color = "#f00"
 })

toRefs

响应式对象转换为普通对象

const state = reactive({
	name: "张三“
})
 return {
    ...toRefs(state)
  }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值