Vue3.5与Vue3新功能对比

一、props解构,默认值

vue3

const { count = 0, message = 'hello' } = withDefaults(
  defineProps<{
    count?: number
    message?: string
  }>(),
  {
    count: 0,
    message: 'hello'
  }
)

vue3.5 

const { count = 0,message = 'hello'} =  defineProps<{
  count?: number;
  message?: string;
}>()

二、useId

全局唯一id-----一个app内 

const id = useId()

三、useTemplateRef

通过ref获取dom

vue3

const button = ref(null)

vue3.5 

const el = useTemplateRef('button')
console.log(el, 'dom')

四、watch新增 onCleanup回调

vue3.5

onCleanup---下一次watch执行前触发---可以在这里移除上一次watch

watch(count, (newVal, oldVal, onCleanup) => {
  const handler = () => {
    console.log(newVal, 'newVal')
  }
  addEventListener('click', handler)
  onCleanup(() => {
    removeEventListener('click', handler)
  })
})

完整代码

<template>
  <div>
    <button ref="button"></button>
  </div>
</template>
<script setup lang="ts">
import { ref, watch, useId, useTemplateRef } from 'vue'
// Vue3
const { count = 0, message = 'hello' } = withDefaults(
  defineProps<{
    count?: number
    message?: string
  }>(),
  {
    count: 0,
    message: 'hello'
  }
)
// Vue3.5
const { count = 0, message = 'hello' } = defineProps<{
  count?: number
  message?: string
}>()
//全局唯一id-----一个app内
const id = useId()
//vue3
const button = ref(null)

//通过ref获取dom----vue3.5
const el = useTemplateRef('button')
console.log(el, 'dom')

// vue3.5
//onCleanup---下一次watch执行前触发---可以在这里移除上一次watch
watch(count, (newVal, oldVal, onCleanup) => {
  const handler = () => {
    console.log(newVal, 'newVal')
  }
  addEventListener('click', handler)
  onCleanup(() => {
    removeEventListener('click', handler)
  })
})
</script>
<style scoped lang="scss"></style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值