1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-23 21:13:07 +03:00
guide/front/client/entry.client.ts

69 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* tslint:disable:ordered-imports */
import 'reflect-metadata'
import 'babel-polyfill'
import Vue from 'vue'
import { createApp } from './app'
const { app, router, store } = createApp()
Vue.mixin({
beforeRouteUpdate (to, from, next) {
const { asyncData } = this.$options
if (typeof asyncData === 'function') {
asyncData.call(this, {
store: this.$store,
route: to
}).then(next).catch(next)
} else {
next()
}
}
})
const STATE_KEY = '__INITIAL_STATE__'
const SSR_ENABLED = window['__SSR_IS_ON__']
if (window[STATE_KEY]) {
store.replaceState(window[STATE_KEY])
}
if (!SSR_ENABLED) {
registerBeforeResolve()
}
router.onReady(() => {
if (SSR_ENABLED) {
registerBeforeResolve()
}
app.$mount('#app')
})
function registerBeforeResolve () {
router.beforeResolve((to, from, next) => {
const matched = router.getMatchedComponents(to)
const prevMatched = router.getMatchedComponents(from)
let diffed = false
const activated = matched.filter((c, i) => {
return diffed || (diffed = (prevMatched[i] !== c))
})
if (!activated.length) {
return next()
}
Promise.all(activated.map(Component => {
const asyncDataFunc = Component['asyncData'] ||
(Component['options'] || {})['asyncData']
if (typeof asyncDataFunc === 'function') {
return asyncDataFunc({ store, route: to })
}
})).then(() => {
next()
}).catch(next)
})
}