fix(nuxt3): handle legacy asyncData (options api) as ref (#2668)

This commit is contained in:
Daniel Roe 2022-01-11 17:41:55 +00:00 committed by GitHub
parent 59e5cfea39
commit 7479eacaac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { defineComponent, getCurrentInstance, toRefs } from 'vue' import { defineComponent, getCurrentInstance, reactive, toRefs } from 'vue'
import type { DefineComponent } from 'vue' import type { DefineComponent } from 'vue'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
import type { LegacyContext } from '../compat/legacy-app' import type { LegacyContext } from '../compat/legacy-app'
@ -14,7 +14,11 @@ async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<str
const { fetchKey } = vm.proxy.$options const { fetchKey } = vm.proxy.$options
const key = typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey || route.fullPath const key = typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey || route.fullPath
const { data } = await useAsyncData(`options:asyncdata:${key}`, () => fn(nuxt._legacyContext)) const { data } = await useAsyncData(`options:asyncdata:${key}`, () => fn(nuxt._legacyContext))
Object.assign(await res, toRefs(data)) if (data.value && typeof data.value === 'object') {
Object.assign(await res, toRefs(reactive(data.value)))
} else if (process.dev) {
console.warn('[nuxt] asyncData should return an object', data)
}
} }
export const defineNuxtComponent: typeof defineComponent = export const defineNuxtComponent: typeof defineComponent =