Fix re-usable component data

This commit is contained in:
Sébastien Chopin 2016-12-20 13:44:00 +01:00
parent 79fae6fe5c
commit 0fe34e0fb0

View File

@ -8,6 +8,7 @@ import { getMatchedComponents, getMatchedComponentsInstances, flatMapComponents,
const noopData = () => { return {} } const noopData = () => { return {} }
const noopFetch = () => {} const noopFetch = () => {}
let _lastPaths = [] let _lastPaths = []
let _lastComponentsFiles = []
function mapTransitions(Components, to, from) { function mapTransitions(Components, to, from) {
return Components.map((Component) => { return Components.map((Component) => {
@ -144,14 +145,15 @@ function render (to, from, next) {
function fixPrepatch (to, ___) { function fixPrepatch (to, ___) {
Vue.nextTick(() => { Vue.nextTick(() => {
let instances = getMatchedComponentsInstances(to) let instances = getMatchedComponentsInstances(to)
instances.forEach((instance, i) => { _lastComponentsFiles = instances.map((instance, i) => {
if (!instance) return; if (!instance) return '';
if (_lastPaths[i] === instance.constructor._path) { if (_lastPaths[i] === instance.constructor._path) {
let newData = instance.constructor.options.data() let newData = instance.constructor.options.data()
for (let key in newData) { for (let key in newData) {
Vue.set(instance.$data, key, newData[key]) Vue.set(instance.$data, key, newData[key])
} }
} }
return instance.constructor.options.__file
}) })
}) })
} }
@ -281,6 +283,7 @@ Promise.all(resolveComponents)
if (Components.length) { if (Components.length) {
_app.setTransitions(mapTransitions(Components, router.currentRoute)) _app.setTransitions(mapTransitions(Components, router.currentRoute))
_lastPaths = router.currentRoute.matched.map((route) => compile(route.path)(router.currentRoute.params)) _lastPaths = router.currentRoute.matched.map((route) => compile(route.path)(router.currentRoute.params))
_lastComponentsFiles = Components.map((Component) => Component.options.__file)
} }
_app.error = _app.$options._nuxt.error.bind(_app) _app.error = _app.$options._nuxt.error.bind(_app)
_app.$loading = {} // to avoid error while _app.$nuxt does not exist _app.$loading = {} // to avoid error while _app.$nuxt does not exist