Nuxt/packages/vue-app/template/router.js

169 lines
6.9 KiB
JavaScript
Raw Normal View History

2016-11-07 01:34:58 +00:00
import Vue from 'vue'
import Router from 'vue-router'
2019-01-06 07:56:59 +00:00
import { interopDefault } from './utils'<%= isTest ? '// eslint-disable-line no-unused-vars' : '' %>
2016-11-07 01:34:58 +00:00
2018-10-24 13:46:06 +00:00
<% function recursiveRoutes(routes, tab, components, indentCount) {
let res = ''
2018-10-24 13:46:06 +00:00
const baseIndent = tab.repeat(indentCount)
const firstIndent = '\n' + tab.repeat(indentCount + 1)
const nextIndent = ',' + firstIndent
2016-12-11 00:46:04 +00:00
routes.forEach((route, i) => {
let resMap = ''
// If need to handle named views
if (route.components) {
let _name = '_' + hash(route.components.default)
if (splitChunks.pages) {
resMap += `${firstIndent}${tab}default: ${_name}`
} else {
resMap += `${firstIndent}${tab}default: () => ${_name}.default || ${_name}`
}
for (const k in route.components) {
_name = '_' + hash(route.components[k])
const component = { _name, component: route.components[k] }
if (k === 'default') {
components.push({
...component,
name: route.name,
chunkName: route.chunkName
})
} else {
components.push({
...component,
name: `${route.name}-${k}`,
chunkName: route.chunkNames[k]
})
if (splitChunks.pages) {
resMap += `${nextIndent}${tab}${k}: ${_name}`
} else {
resMap += `${nextIndent}${tab}${k}: () => ${_name}.default || ${_name}`
}
}
}
route.component = false
} else {
route._name = '_' + hash(route.component)
components.push({ _name: route._name, component: route.component, name: route.name, chunkName: route.chunkName })
}
// @see: https://router.vuejs.org/api/#router-construction-options
2018-10-24 13:46:06 +00:00
res += '{'
res += firstIndent + 'path: ' + JSON.stringify(route.path)
res += (route.components) ? nextIndent + 'components: {' + resMap + '\n' + baseIndent + tab + '}' : ''
2018-10-24 13:46:06 +00:00
res += (route.component) ? nextIndent + 'component: ' + (splitChunks.pages ? route._name : `() => ${route._name}.default || ${route._name}`) : ''
res += (route.redirect) ? nextIndent + 'redirect: ' + JSON.stringify(route.redirect) : ''
res += (route.meta) ? nextIndent + 'meta: ' + JSON.stringify(route.meta) : ''
res += (typeof route.props !== 'undefined') ? nextIndent + 'props: ' + (typeof route.props === 'function' ? serialize(route.props) : JSON.stringify(route.props)) : ''
res += (typeof route.caseSensitive !== 'undefined') ? nextIndent + 'caseSensitive: ' + JSON.stringify(route.caseSensitive) : ''
res += (route.alias) ? nextIndent + 'alias: ' + JSON.stringify(route.alias) : ''
res += (route.pathToRegexpOptions) ? nextIndent + 'pathToRegexpOptions: ' + JSON.stringify(route.pathToRegexpOptions) : ''
res += (route.name) ? nextIndent + 'name: ' + JSON.stringify(route.name) : ''
if (route.beforeEnter) {
if(isTest) { res += ',\n/* eslint-disable indent, semi */' }
res += (isTest ? firstIndent : nextIndent) + 'beforeEnter: ' + serialize(route.beforeEnter)
if(isTest) { res += firstIndent + '/* eslint-enable indent, semi */' }
}
res += (route.children) ? nextIndent + 'children: [' + recursiveRoutes(routes[i].children, tab, components, indentCount + 1) + ']' : ''
res += '\n' + baseIndent + '}' + (i + 1 === routes.length ? '' : ', ')
2016-12-11 00:46:04 +00:00
})
return res
}
2017-07-09 22:40:09 +00:00
const _components = []
2018-10-24 13:46:06 +00:00
const _routes = recursiveRoutes(router.routes, ' ', _components, 2)
%><%= uniqBy(_components, '_name').map((route) => {
if (!route.component) return ''
2018-03-13 14:36:49 +00:00
const path = relativeToBuild(route.component)
const chunkName = wChunk(route.chunkName)
const name = route._name
if (splitChunks.pages) {
return `const ${name} = () => interopDefault(import('${path}' /* webpackChunkName: "${chunkName}" */))`
2018-03-13 14:36:49 +00:00
} else {
return `import ${name} from '${path}'`
}
}).join('\n')%>
Vue.use(Router)
2016-11-07 01:34:58 +00:00
2017-01-26 14:56:47 +00:00
<% if (router.scrollBehavior) { %>
const scrollBehavior = <%= serializeFunction(router.scrollBehavior) %>
2017-01-26 14:56:47 +00:00
<% } else { %>
2018-11-08 09:15:56 +00:00
if (process.client) {
if ('scrollRestoration' in window.history) {
window.history.scrollRestoration = 'manual'
// reset scrollRestoration to auto when leaving page, allowing page reload
// and back-navigation from other pages to use the browser to restore the
// scrolling position.
window.addEventListener('beforeunload', () => {
window.history.scrollRestoration = 'auto'
})
// Setting scrollRestoration to manual again when returning to this page.
window.addEventListener('load', () => {
window.history.scrollRestoration = 'manual'
})
}
}
const scrollBehavior = function (to, from, savedPosition) {
// if the returned position is falsy or an empty object,
// will retain current scroll position.
let position = false
// if no children detected and scrollToTop is not explicitly disabled
if (
to.matched.length < 2 &&
to.matched.every(r => r.components.default.options.scrollToTop !== false)
) {
// scroll to the top of the page
position = { x: 0, y: 0 }
2018-10-24 13:46:06 +00:00
} else if (to.matched.some(r => r.components.default.options.scrollToTop)) {
// if one of the children has scrollToTop option set to true
position = { x: 0, y: 0 }
}
// savedPosition is only available for popstate navigations (back button)
2016-11-07 01:34:58 +00:00
if (savedPosition) {
position = savedPosition
2016-11-07 01:34:58 +00:00
}
return new Promise((resolve) => {
// wait for the out transition to complete (if necessary)
window.<%= globals.nuxt %>.$once('triggerScroll', () => {
// coords will be used if no selector is provided,
// or if the selector didn't match any element.
if (to.hash) {
let hash = to.hash
// CSS.escape() is not supported with IE and Edge.
if (typeof window.CSS !== 'undefined' && typeof window.CSS.escape !== 'undefined') {
hash = '#' + window.CSS.escape(hash.substr(1))
}
try {
if (document.querySelector(hash)) {
// scroll to anchor by returning the selector
position = { selector: hash }
}
} catch (e) {
2018-03-01 14:59:56 +00:00
console.warn('Failed to save scroll position. Please add CSS.escape() polyfill (https://github.com/mathiasbynens/CSS.escape).')
}
}
resolve(position)
})
})
2016-11-07 01:34:58 +00:00
}
2017-01-26 14:56:47 +00:00
<% } %>
2016-11-07 01:34:58 +00:00
2018-10-24 13:46:06 +00:00
export function createRouter() {
2017-05-02 06:57:39 +00:00
return new Router({
mode: '<%= router.mode %>',
base: '<%= router.base %>',
linkActiveClass: '<%= router.linkActiveClass %>',
linkExactActiveClass: '<%= router.linkExactActiveClass %>',
2017-05-02 06:57:39 +00:00
scrollBehavior,
2019-01-06 07:56:59 +00:00
<%= isTest ? '/* eslint-disable quotes, object-curly-spacing, key-spacing */' : '' %>
2018-10-24 13:46:06 +00:00
routes: [<%= _routes %>],
2019-01-06 07:56:59 +00:00
<%= isTest ? '/* eslint-enable quotes, object-curly-spacing, key-spacing */' : '' %>
<% if (router.parseQuery) { %>parseQuery: <%= serializeFunction(router.parseQuery) %>,<% } %>
<% if (router.stringifyQuery) { %>stringifyQuery: <%= serializeFunction(router.stringifyQuery) %>,<% } %>
2017-07-04 16:30:01 +00:00
fallback: <%= router.fallback %>
2017-05-02 06:57:39 +00:00
})
}