Nuxt/lib/app/router.js

66 lines
2.2 KiB
JavaScript
Raw Normal View History

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) {
2017-07-09 22:40:09 +00:00
let res = ''
2016-12-11 00:46:04 +00:00
routes.forEach((route, i) => {
2017-01-19 15:25:55 +00:00
route._name = '_' + hash(route.component)
2017-04-17 16:15:37 +00:00
components.push({ _name: route._name, component: route.component, name: route.name })
2016-12-11 00:46:04 +00:00
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
}
2017-07-09 22:40:09 +00:00
const _components = []
const _routes = recursiveRoutes(router.routes, '\t\t', _components)
2017-08-17 16:59:17 +00:00
uniqBy(_components, '_name').forEach((route) => { %>const <%= route._name %> = () => import('<%= relativeToBuild(route.component) %>' /* webpackChunkName: "pages/<%= wp(route.name) %>" */).then(m => m.default || m)
2016-11-07 01:34:58 +00:00
<% }) %>
2017-01-26 14:56:47 +00:00
<% if (router.scrollBehavior) { %>
const scrollBehavior = <%= serialize(router.scrollBehavior).replace('scrollBehavior(', 'function(') %>
<% } else { %>
2016-11-07 01:34:58 +00:00
const scrollBehavior = (to, from, savedPosition) => {
2017-07-09 22:40:09 +00:00
// SavedPosition is only available for popstate navigations.
2016-11-07 01:34:58 +00:00
if (savedPosition) {
return savedPosition
} else {
2016-12-11 15:40:18 +00:00
let position = {}
2017-07-09 22:40:09 +00:00
// If no children detected
2016-12-11 15:40:18 +00:00
if (to.matched.length < 2) {
2017-07-09 22:40:09 +00:00
// Scroll to the top of the page
2016-12-11 15:40:18 +00:00
position = { x: 0, y: 0 }
}
2017-01-20 17:32:43 +00:00
else if (to.matched.some((r) => r.components.default.options.scrollToTop)) {
2017-07-09 22:40:09 +00:00
// If one of the children has scrollToTop option set to true
2016-12-27 13:53:14 +00:00
position = { x: 0, y: 0 }
}
2017-07-09 22:40:09 +00:00
// If link has anchor, scroll to anchor by returning the selector
2016-11-07 01:34:58 +00:00
if (to.hash) {
position = { selector: to.hash }
}
return position
}
}
2017-01-26 14:56:47 +00:00
<% } %>
2016-11-07 01:34:58 +00:00
2017-05-02 06:57:39 +00:00
export function createRouter () {
return new Router({
mode: '<%= router.mode %>',
base: '<%= router.base %>',
linkActiveClass: '<%= router.linkActiveClass %>',
linkExactActiveClass: '<%= router.linkExactActiveClass %>',
2017-05-02 06:57:39 +00:00
scrollBehavior,
routes: [
2017-07-09 22:40:09 +00:00
<%= _routes %>
2017-07-04 16:30:01 +00:00
],
fallback: <%= router.fallback %>
2017-05-02 06:57:39 +00:00
})
}