Nuxt/lib/app/components/nuxt-child.js
ousiri d603b0d418 feat: allow nuxt-child to pass keep-alive props to keep-alive (#4067)
Adds the ability to configure the props of keep-alive on nuxt-child.

## Types of changes
- [ ] Bug fix (a non-breaking change which fixes an issue)
- [x] New feature (a non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)


## Description
In development, I found it impossible to configure the props of keep-alive when using nuxt-child, like ```exclude```. This PR adds a special prop to nuxt-child named keepAliveProps to allow developer to configure it.

```html
<template>
  <div>
    <nuxt-child keep-alive :keep-alive-props="{ exclude: ['modal'] }">
  </div>
</template>
```

## Checklist:
- [x] My change requires a change to the documentation.
- [x] I have updated the documentation accordingly. (PR: https://github.com/nuxt/docs/pull/836)
- [ ] I have added tests to cover my changes (if not applicable, please state why)
- [x] All new and existing tests are passing.
2018-10-08 14:27:36 +01:00

90 lines
2.1 KiB
JavaScript

export default {
name: 'nuxt-child',
functional: true,
props: ['keepAlive', 'keepAliveProps'],
render (h, { parent, data, props }) {
data.nuxtChild = true
const _parent = parent
const transitions = parent.$nuxt.nuxt.transitions
const defaultTransition = parent.$nuxt.nuxt.defaultTransition
let depth = 0
while (parent) {
if (parent.$vnode && parent.$vnode.data.nuxtChild) {
depth++
}
parent = parent.$parent
}
data.nuxtChildDepth = depth
const transition = transitions[depth] || defaultTransition
let transitionProps = {}
transitionsKeys.forEach((key) => {
if (typeof transition[key] !== 'undefined') {
transitionProps[key] = transition[key]
}
})
let listeners = {}
listenersKeys.forEach((key) => {
if (typeof transition[key] === 'function') {
listeners[key] = transition[key].bind(_parent)
}
})
// Add triggerScroll event on beforeEnter (fix #1376)
let beforeEnter = listeners.beforeEnter
listeners.beforeEnter = (el) => {
// Ensure to trigger scroll event after calling scrollBehavior
window.$nuxt.$nextTick(() => {
window.$nuxt.$emit('triggerScroll')
})
if (beforeEnter) return beforeEnter.call(_parent, el)
}
let routerView = [
h('router-view', data)
]
if (typeof props.keepAlive !== 'undefined') {
routerView = [
h('keep-alive', { props: props.keepAliveProps }, routerView)
]
}
return h('transition', {
props: transitionProps,
on: listeners
}, routerView)
}
}
const transitionsKeys = [
'name',
'mode',
'appear',
'css',
'type',
'duration',
'enterClass',
'leaveClass',
'appearClass',
'enterActiveClass',
'enterActiveClass',
'leaveActiveClass',
'appearActiveClass',
'enterToClass',
'leaveToClass',
'appearToClass'
]
const listenersKeys = [
'beforeEnter',
'enter',
'afterEnter',
'enterCancelled',
'beforeLeave',
'leave',
'afterLeave',
'leaveCancelled',
'beforeAppear',
'appear',
'afterAppear',
'appearCancelled'
]