Normalize components

This commit is contained in:
Sébastien Chopin 2017-01-20 18:32:43 +01:00
parent 224a18dea8
commit 55f907877e
2 changed files with 15 additions and 7 deletions

View File

@ -36,12 +36,6 @@ function loadAsyncComponents (to, ___, next) {
Component().then(_resolve).catch(reject)
})
}
if (typeof Component === 'object' && !Component.options) {
// Updated via vue-router resolveAsyncComponents()
Component = Vue.extend(Component)
Component._Ctor = Component
match.components[key] = Component
}
return Component
})
<%= (loading ? 'this.$loading.start && this.$loading.start()' : '') %>
@ -155,6 +149,19 @@ function render (to, from, next) {
})
}
// Fix components format in matched, it's due to code-splitting of vue-router
function normalizeComponents (to, ___) {
flatMapComponents(to, (Component, _, match, key) => {
if (typeof Component === 'object' && !Component.options) {
// Updated via vue-router resolveAsyncComponents()
Component = Vue.extend(Component)
Component._Ctor = Component
match.components[key] = Component
}
return Component
})
}
// When navigating on a different route but the same component is used, Vue.js
// will not update the instance data, so we have to update $data ourselves
function fixPrepatch (to, ___) {
@ -329,6 +336,7 @@ Promise.all(resolveComponents)
// Add router hooks
router.beforeEach(loadAsyncComponents.bind(_app))
router.beforeEach(render.bind(_app))
router.afterEach(normalizeComponents)
router.afterEach(fixPrepatch.bind(_app))
if (NUXT.serverRendered) {
mountApp()

View File

@ -36,7 +36,7 @@ const scrollBehavior = (to, from, savedPosition) => {
if (to.matched.length < 2) {
position = { x: 0, y: 0 }
}
else if (to.matched.some((r) => r.components.default.scrollToTop || (r.components.default.options && r.components.default.options.scrollToTop))) {
else if (to.matched.some((r) => r.components.default.options.scrollToTop)) {
position = { x: 0, y: 0 }
}
// if link has anchor, scroll to anchor by returning the selector