feat: respect beforeEnter for route if set (#3760)

* feat: respect beforeEnter for route if set

* test: add redirecting beforeEnter function and test
This commit is contained in:
Alexander Lichter 2018-08-18 18:15:37 +02:00 committed by Sébastien Chopin
parent 8486ce4feb
commit e26f23b74a
3 changed files with 15 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import Router from 'vue-router'
res += (route.redirect) ? ',\n\t' + tab + 'redirect: ' + JSON.stringify(route.redirect) : ''
res += (route.meta) ? ',\n\t' + tab + 'meta: ' + JSON.stringify(route.meta) : ''
res += (route.name) ? ',\n\t' + tab + 'name: ' + JSON.stringify(route.name) : ''
res += (route.beforeEnter) ? ',\n\t' + tab + 'beforeEnter: ' + serialize(route.beforeEnter) : ''
res += (route.children) ? ',\n\t' + tab + 'children: [\n' + recursiveRoutes(routes[i].children, tab + '\t\t', components) + '\n\t' + tab + ']' : ''
res += '\n' + tab + '}' + (i + 1 === routes.length ? '' : ',\n')
})

View File

@ -6,6 +6,15 @@ export default {
maxAge: ((60 * 60 * 24 * 365) * 2)
}
},
router: {
extendRoutes(routes, resolve) {
return [{
path: '/before-enter',
name: 'before-enter',
beforeEnter: (to, from, next) => { next('/') }
}, ...routes]
}
},
generate: {
routes: [
// TODO: generate with {build: false} does not scans pages!

View File

@ -101,6 +101,11 @@ describe('basic ssr', () => {
expect(html.includes('<h1>I am valid</h1>')).toBe(true)
})
test('/before-enter', async () => {
const { html } = await nuxt.renderRoute('/before-enter')
expect(html.includes('<h1>Index page</h1>')).toBe(true)
})
test('/redirect', async () => {
const { html, redirected } = await nuxt.renderRoute('/redirect')
expect(html.includes('<div id="__nuxt"></div>')).toBe(true)