MCPcopy
hub / github.com/nuxt/nuxt / useNuxtData

Function useNuxtData

packages/nuxt/src/app/composables/asyncData.ts:605–641  ·  view source on GitHub ↗
(key: string)

Source from the content-addressed store, hash-verified

603
604/** @since 3.1.0 */
605export function useNuxtData<DataT = any> (key: string): { data: Ref<DataT | undefined> } {
606 const nuxtApp = useNuxtApp()
607
608 // Initialize value when key is not already set
609 if (!(key in nuxtApp.payload.data)) {
610 nuxtApp.payload.data[key] = undefined
611 }
612
613 if (nuxtApp._asyncData[key]) {
614 const data = nuxtApp._asyncData[key]
615 data._deps++
616 if (getCurrentScope()) {
617 onScopeDispose(() => {
618 data._deps--
619 // clean up memory when it no longer is needed
620 if (data._deps === 0) {
621 data?._off()
622 }
623 })
624 }
625 }
626
627 return {
628 data: computed({
629 get () {
630 return nuxtApp._asyncData[key]?.data.value ?? nuxtApp.payload.data[key]
631 },
632 set (value) {
633 if (nuxtApp._asyncData[key]) {
634 nuxtApp._asyncData[key]!.data.value = value
635 } else {
636 nuxtApp.payload.data[key] = value
637 }
638 },
639 }),
640 }
641}
642
643/** @since 3.0.0 */
644export async function refreshNuxtData (keys?: string | string[]): Promise<void> {

Callers 2

setupFunction · 0.90

Calls 1

useNuxtAppFunction · 0.90

Tested by 1

setupFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…