add nuxt-child and nuxt-link

This commit is contained in:
Sébastien Chopin 2016-12-16 17:45:47 +01:00
parent 213ee890a7
commit 2cf68b6019
6 changed files with 98 additions and 53 deletions

View File

@ -0,0 +1,60 @@
import Vue from 'vue'
const transitionsKeys = [
'name',
'mode',
'css',
'type',
'enterClass',
'leaveClass',
'enterActiveClass',
'leaveActiveClass'
]
const listenersKeys = [
'beforeEnter',
'enter',
'afterEnter',
'enterCancelled',
'beforeLeave',
'leave',
'afterLeave',
'leaveCancelled'
]
export default {
name: 'nuxt-child',
functional: true,
render (h, { parent, data }) {
data.nuxtChild = true
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]
}
})
return h('transition', {
props: transitionProps,
on: listeners
}, [
h('router-view', data)
])
}
}

View File

@ -1,36 +0,0 @@
<template>
<transition :name="transition.name" :mode="transition.mode">
<router-view></router-view>
</transition>
</template>
<script>
export default {
name: 'nuxt-child',
props: {
transition: {
type: Object,
default: () => {
return {
hooks: false,
mode: 'out-in', // out-in ou in-out
name: 'fade',
type: '', // animation ou transition
enterClass: '',
enterActiveClass: '',
leaveClass: '',
leaveActiveClass: '',
beforeEnter: null, // Func
enter: null,
afterEnter: null,
enterCancelled: null,
beforeLeave: null,
leave: null,
afterLeave: null,
leaveCancelled: null
}
}
}
}
}
</script>

View File

@ -0,0 +1,9 @@
import Vue from 'vue'
export default {
name: 'nuxt-link',
functional: true,
render (h, { data, children }) {
return h('router-link', data, children)
}
}

View File

@ -1,15 +1,14 @@
<template> <template>
<div> <div>
<% if (loading) { %><nuxt-loading ref="loading"></nuxt-loading><% } %> <% if (loading) { %><nuxt-loading ref="loading"></nuxt-loading><% } %>
<transition :name="nuxt.transition.name" :mode="nuxt.transition.mode"> <nuxt-child v-if="!nuxt.err"></nuxt-child>
<router-view v-if="!nuxt.err"></router-view>
<nuxt-error v-if="nuxt.err" :error="nuxt.err"></nuxt-error> <nuxt-error v-if="nuxt.err" :error="nuxt.err"></nuxt-error>
</transition>
</div> </div>
</template> </template>
<script> <script>
import Vue from 'vue' import Vue from 'vue'
import NuxtChild from './nuxt-child'
import NuxtError from '<%= components.ErrorPage %>' import NuxtError from '<%= components.ErrorPage %>'
<% if (loading) { %>import NuxtLoading from '<%= (typeof loading === "string" ? loading : "./nuxt-loading.vue") %>'<% } %> <% if (loading) { %>import NuxtLoading from '<%= (typeof loading === "string" ? loading : "./nuxt-loading.vue") %>'<% } %>
@ -47,6 +46,7 @@ export default {
}, },
<% } %> <% } %>
components: { components: {
NuxtChild,
NuxtError<%= (loading ? ',\n NuxtLoading' : '') %> NuxtError<%= (loading ? ',\n NuxtLoading' : '') %>
} }
} }

View File

@ -5,7 +5,8 @@ import Meta from 'vue-meta'
import router from './router.js' import router from './router.js'
<% if (store) { %>import store from '~store/index.js'<% } %> <% if (store) { %>import store from '~store/index.js'<% } %>
import NuxtContainer from './components/nuxt-container.vue' import NuxtContainer from './components/nuxt-container.vue'
import NuxtChild from './components/nuxt-child.vue' import NuxtChild from './components/nuxt-child.js'
import NuxtLink from './components/nuxt-link.js'
import Nuxt from './components/nuxt.vue' import Nuxt from './components/nuxt.vue'
import App from '<%= appPath %>' import App from '<%= appPath %>'
@ -13,6 +14,8 @@ import App from '<%= appPath %>'
Vue.component(NuxtContainer.name, NuxtContainer) Vue.component(NuxtContainer.name, NuxtContainer)
// Component: <nuxt-child> // Component: <nuxt-child>
Vue.component(NuxtChild.name, NuxtChild) Vue.component(NuxtChild.name, NuxtChild)
// Component: <nuxt-link>
Vue.component(NuxtLink.name, NuxtLink)
// Component: <nuxt> // Component: <nuxt>
Vue.component(Nuxt.name, Nuxt) Vue.component(Nuxt.name, Nuxt)
@ -46,16 +49,24 @@ const app = {
router, router,
<%= (store ? 'store,' : '') %> <%= (store ? 'store,' : '') %>
_nuxt: { _nuxt: {
transition: Object.assign({}, defaultTransition), defaultTransition: defaultTransition,
setTransition (transition) { transitions: [ defaultTransition ],
setTransitions (transitions) {
if (!Array.isArray(transitions)) {
transitions = [ transitions ]
}
transitions = transitions.map((transition) => {
if (!transition) { if (!transition) {
transition = defaultTransition transition = defaultTransition
} else if (typeof transition === 'string') { } else if (typeof transition === 'string') {
transition = Object.assign({}, defaultTransition, { name: transition }) transition = Object.assign({}, defaultTransition, { name: transition })
} else {
transition = Object.assign({}, defaultTransition, transition)
} }
this.$options._nuxt.transition.name = transition.name
this.$options._nuxt.transition.mode = transition.mode
return transition return transition
})
this.$options._nuxt.transitions = transitions
return transitions
}, },
err: null, err: null,
error (err) { error (err) {

View File

@ -151,10 +151,11 @@ function * generateRoutesAndFiles () {
'router.js', 'router.js',
'server.js', 'server.js',
'utils.js', 'utils.js',
'components/nuxt-child.vue',
'components/nuxt-container.vue', 'components/nuxt-container.vue',
'components/nuxt.vue', 'components/nuxt-loading.vue',
'components/nuxt-loading.vue' 'components/nuxt-child.js',
'components/nuxt-link.js',
'components/nuxt.vue'
] ]
let templateVars = { let templateVars = {
uniqBy: _.uniqBy, uniqBy: _.uniqBy,