fix(nuxt): warn if inheritAttrs: false when using useId (#25616)

This commit is contained in:
Daniel Roe 2024-02-05 10:30:59 +00:00 committed by GitHub
parent b78e1cb206
commit 8705aed8b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -29,6 +29,9 @@ export function useId (key?: string): string {
const instanceIndex = key + ':' + instance._nuxtIdIndex[key]++
if (import.meta.server) {
if (import.meta.dev && instance.vnode.type && typeof instance.vnode.type === 'object' && 'inheritAttrs' in instance.vnode.type && instance.vnode.type.inheritAttrs === false) {
console.warn('[nuxt] `useId` is not compatible with components that have `inheritAttrs: false`.')
}
const ids = JSON.parse(instance.attrs[ATTR_KEY] as string | undefined || '{}')
ids[instanceIndex] = key + ':' + nuxtApp._id++
instance.attrs[ATTR_KEY] = JSON.stringify(ids)