useId() 组合式API返回一个Vue引用,其中包含一个可用作DOM节点属性唯一标识符的字符串。
如果您提供一个函数(下面类型定义中的getValue)来获取ID可能具有的值,它将确保保持更新。
在SSR中,它考虑了水合(hydration)过程,因此您的组件不会生成任何此类错误。
语法
import { useId } from 'quasar'
setup () {
const id = useId()
// ...
}content_paste
function useId(
opts?: {
getValue?: () => string | null | undefined;
required?: boolean; // 默认值: true
}
): Ref<string | null>;content_paste
示例
<template>
<div :id="id">某个组件</div>
</template>
<script setup>
import { useId } from 'quasar'
const props = defineProps({
for: String
})
const id = useId({
getValue: () => props.for,
required: true
})
</script>content_paste