useUid - generate unique id for a Vue instance
See original GitHub issueWhen authoring accessible components, sometimes you need an unique id to use on eg. <label for=""/>
elements or for aria-describedby
attrs
<template>
<div id="description">This is a deadly {{animal}}</div>
<span aria-describedby="dsecription">{{ animal ==== 'snake' ? 'sssssssssss' : 'bark?' }}</span>
</template>
The above example is wrong because what if you use the above component many times in the same wrapper component? The id=“description” occurs several times and is not uniquely tied to one single, correct description
That can be fixed by doing :id=“uid”, where uid is the uniqe id generated during created() lifecycle hook
Propsal:
<script setup>
const {uid} = useUid();
</script>
I think it’s worth providing it as a composable that you have to destructure rather than a raw function to prevent people from calling uuid() manually in the template (which I guess could generate a different id on rerenders, especially if you used uuid() multiple times in the template, with const {uuid} = useUuid() it’s harder to run into that problem)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Came here kind of looking for the same thing. Here’s another option:
Also for who just wants a simple unique id, that does not repeat, and not need to be uuid
use-id.js