mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
feat(app): backwards-compatible options asyncData
support (#286)
This commit is contained in:
parent
79523399d2
commit
53f6756178
13
examples/async-data/app.vue
Normal file
13
examples/async-data/app.vue
Normal file
@ -0,0 +1,13 @@
|
||||
<template>
|
||||
<div>
|
||||
<div>
|
||||
<nuxt-page />
|
||||
</div>
|
||||
<nuxt-link to="/">
|
||||
Home
|
||||
</nuxt-link>
|
||||
<nuxt-link to="/options">
|
||||
Options
|
||||
</nuxt-link>
|
||||
</div>
|
||||
</template>
|
31
examples/async-data/pages/options.vue
Normal file
31
examples/async-data/pages/options.vue
Normal file
@ -0,0 +1,31 @@
|
||||
<template>
|
||||
<div>
|
||||
About
|
||||
{{ path }}
|
||||
<button @click="path = 'newpath'">
|
||||
Update path
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineNuxtComponent } from '@nuxt/app'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export default defineNuxtComponent({
|
||||
fetchKey: 'custom',
|
||||
asyncData ({ route }) {
|
||||
return {
|
||||
path: route.path
|
||||
}
|
||||
},
|
||||
setup () {
|
||||
// This will get overwritten with asyncData
|
||||
const path = ref('original path')
|
||||
|
||||
return {
|
||||
path
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
@ -1,4 +1,9 @@
|
||||
import { toRefs } from '@vue/reactivity'
|
||||
import { ComponentInternalInstance, DefineComponent, defineComponent, getCurrentInstance } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import type { LegacyContext } from '../legacy'
|
||||
import { useNuxt } from '../nuxt'
|
||||
import { asyncData } from './asyncData'
|
||||
|
||||
export const NuxtComponentIndicator = '__nuxt_component'
|
||||
export const NuxtComponentPendingPromises = '_pendingPromises'
|
||||
@ -22,11 +27,21 @@ export function enqueueNuxtComponent (p: Promise<void>) {
|
||||
vm[NuxtComponentPendingPromises].push(p)
|
||||
}
|
||||
|
||||
async function runLegacyAsyncData (res: Record<string, any> | Promise<Record<string, any>>, fn: (context: LegacyContext) => Promise<Record<string, any>>) {
|
||||
const nuxt = useNuxt()
|
||||
const route = useRoute()
|
||||
const vm = getCurrentNuxtComponentInstance()
|
||||
const { fetchKey } = vm.proxy.$options
|
||||
const key = typeof fetchKey === 'function' ? fetchKey(() => '') : fetchKey || route.fullPath
|
||||
const { data } = await asyncData(`options:asyncdata:${key}`, () => fn(nuxt._legacyContext))
|
||||
Object.assign(await res, toRefs(data))
|
||||
}
|
||||
|
||||
export const defineNuxtComponent: typeof defineComponent =
|
||||
function defineNuxtComponent (options: any): any {
|
||||
const { setup } = options
|
||||
|
||||
if (!setup) {
|
||||
if (!setup && !options.asyncData) {
|
||||
return {
|
||||
[NuxtComponentIndicator]: true,
|
||||
...options
|
||||
@ -40,7 +55,11 @@ export const defineNuxtComponent: typeof defineComponent =
|
||||
const vm = getCurrentNuxtComponentInstance()
|
||||
let promises = vm[NuxtComponentPendingPromises] = vm[NuxtComponentPendingPromises] || []
|
||||
|
||||
const res = setup(props, ctx)
|
||||
const res = setup?.(props, ctx) || {}
|
||||
|
||||
if (options.asyncData) {
|
||||
promises.push(runLegacyAsyncData(res, options.asyncData))
|
||||
}
|
||||
|
||||
if (!promises.length && !(res instanceof Promise)) {
|
||||
return res
|
||||
|
Loading…
Reference in New Issue
Block a user