mirror of
https://github.com/aelve/guide.git
synced 2024-11-23 21:13:07 +03:00
Added call of asyncData functions of children of resolved component while server rendering
This commit is contained in:
parent
e904a023f8
commit
216c18e485
@ -2,13 +2,13 @@ import 'reflect-metadata'
|
|||||||
import _get from 'lodash/get'
|
import _get from 'lodash/get'
|
||||||
import { createApp } from './app'
|
import { createApp } from './app'
|
||||||
|
|
||||||
export default context => {
|
export default async context => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const { app, router, store } = createApp()
|
const { app, router, store } = createApp()
|
||||||
|
|
||||||
router.push(context.url)
|
router.push(context.url)
|
||||||
|
|
||||||
router.onReady(() => {
|
router.onReady(async () => {
|
||||||
const matchedComponents = router.getMatchedComponents()
|
const matchedComponents = router.getMatchedComponents()
|
||||||
|
|
||||||
if (!matchedComponents.length) {
|
if (!matchedComponents.length) {
|
||||||
@ -17,21 +17,43 @@ export default context => {
|
|||||||
error: new Error('no component matched')
|
error: new Error('no component matched')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
const matchedComponentsAndChildren = matchedComponents
|
||||||
|
.reduce((acc, matchedComponent) => {
|
||||||
|
const componentAndItsChildren = getComponentAndItsChildren(matchedComponent)
|
||||||
|
acc = acc.concat(componentAndItsChildren)
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
|
||||||
Promise.all(matchedComponents.map((Component) => {
|
await Promise.all(matchedComponentsAndChildren.map((Component) => {
|
||||||
const asyncDataFunc = Component['asyncData']
|
const asyncDataFunc = _get(Component, 'options.methods.asyncData')
|
||||||
|| _get(Component, 'options.asyncData')
|
|
||||||
|| _get(Component, 'options.methods.asyncData')
|
|
||||||
if (typeof asyncDataFunc === 'function') {
|
if (typeof asyncDataFunc === 'function') {
|
||||||
return asyncDataFunc({
|
return asyncDataFunc({
|
||||||
store,
|
store,
|
||||||
route: router.currentRoute
|
route: router.currentRoute
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})).then(() => {
|
}))
|
||||||
context.state = store.state
|
context.state = store.state
|
||||||
resolve(app)
|
resolve(app)
|
||||||
}).catch(reject)
|
} catch (e) {
|
||||||
|
reject
|
||||||
|
}
|
||||||
}, reject)
|
}, reject)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getComponentAndItsChildren(component, result?) {
|
||||||
|
if (!result) {
|
||||||
|
result = []
|
||||||
|
}
|
||||||
|
if (!result.includes(component)) {
|
||||||
|
result.push(component)
|
||||||
|
}
|
||||||
|
const children = Object.values(component.options.components)
|
||||||
|
// Parent component is also presents in components object
|
||||||
|
.filter(x => x !== component)
|
||||||
|
children.forEach(x => getComponentAndItsChildren(x, result))
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user