Nuxt/lib/app/router.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict'
2016-11-07 01:34:58 +00:00
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
2016-12-11 00:46:04 +00:00
<%
2016-12-12 15:30:07 +00:00
function recursiveRoutes(routes, tab, components) {
var res = ''
2016-12-11 00:46:04 +00:00
routes.forEach((route, i) => {
components.push({ _name: route._name, component: route.component })
res += tab + '{\n'
res += tab + '\tpath: ' + JSON.stringify(route.path) + ',\n'
res += tab + '\tcomponent: ' + route._name
res += (route.name) ? ',\n\t' + tab + 'name: ' + JSON.stringify(route.name) : ''
2016-12-12 15:30:07 +00:00
res += (route.children) ? ',\n\t' + tab + 'children: [\n' + recursiveRoutes(routes[i].children, tab + '\t\t', components) + '\n\t' + tab + ']' : ''
2016-12-11 00:46:04 +00:00
res += '\n' + tab + '}' + (i + 1 === routes.length ? '' : ',\n')
})
return res
}
2016-12-12 15:30:07 +00:00
var _components = []
var _routes = recursiveRoutes(router.routes, '\t\t', _components)
uniqBy(_components, '_name').forEach((route) => { %>
2016-12-11 00:46:04 +00:00
const <%= route._name %> = process.BROWSER_BUILD ? () => System.import('<%= route.component %>') : require('<%= route.component %>')
2016-11-07 01:34:58 +00:00
<% }) %>
const scrollBehavior = (to, from, savedPosition) => {
if (savedPosition) {
// savedPosition is only available for popstate navigations.
return savedPosition
} else {
2016-12-11 15:40:18 +00:00
let position = {}
// if no children detected
if (to.matched.length < 2) {
position = { x: 0, y: 0 }
}
2016-12-27 13:53:14 +00:00
else if (to.matched.some((r) => r.components.default.scrollToTop || (r.components.default.options && r.components.default.options.scrollToTop))) {
position = { x: 0, y: 0 }
}
2016-11-07 01:34:58 +00:00
// if link has anchor, scroll to anchor by returning the selector
if (to.hash) {
position = { selector: to.hash }
}
return position
}
}
export default new Router({
mode: 'history',
2016-11-10 16:16:37 +00:00
base: '<%= router.base %>',
linkActiveClass: '<%= router.linkActiveClass %>',
2016-11-07 01:34:58 +00:00
scrollBehavior,
routes: [
2016-12-12 15:30:07 +00:00
<%= _routes %>
2016-11-07 01:34:58 +00:00
]
})