vue3-core/packages/runtime-core/src/helpers/useId.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

29 lines
735 B
TypeScript
Raw Normal View History

2024-07-19 18:06:02 +08:00
import {
type ComponentInternalInstance,
getCurrentInstance,
} from '../component'
import { warn } from '../warning'
export function useId(): string {
2024-07-19 18:06:02 +08:00
const i = getCurrentInstance()
if (i) {
return (i.appContext.config.idPrefix || 'v') + '-' + i.ids[0] + i.ids[1]++
2024-07-19 18:06:02 +08:00
} else if (__DEV__) {
warn(
`useId() is called when there is no active component ` +
`instance to be associated with.`,
)
}
return ''
2024-07-19 18:06:02 +08:00
}
/**
* There are 3 types of async boundaries:
* - async components
* - components with async setup()
* - components with serverPrefetch
*/
export function markAsyncBoundary(instance: ComponentInternalInstance): void {
2024-07-19 18:06:02 +08:00
instance.ids = [instance.ids[0] + instance.ids[2]++ + '-', 0, 0]
}