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() 通过ref获取dom
vue3
const button = ref(null) 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) }) }) 完整代码