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

257 lines
7.7 KiB
JavaScript
Raw Normal View History

import Vue from 'vue'
<% if (features.asyncData || features.fetch) { %>
import {
getMatchedComponentsInstances,
promisify,
globalHandleError
} from './utils'
<% } %>
<% if (loading) { %>import NuxtLoading from '<%= (typeof loading === "string" ? loading : "./components/nuxt-loading.vue") %>'<% } %>
<% if (buildIndicator) { %>import NuxtBuildIndicator from './components/nuxt-build-indicator'<% } %>
2018-08-15 13:23:03 +00:00
<% css.forEach((c) => { %>
2019-01-02 21:30:19 +00:00
import '<%= relativeToBuild(resolvePath(c.src || c, { isStyle: true })) %>'
<% }) %>
<% if (features.layouts) { %>
<%= Object.keys(layouts).map((key) => {
if (splitChunks.layouts) {
return `const _${hash(key)} = () => import('${layouts[key]}' /* webpackChunkName: "${wChunk('layouts/' + key)}" */).then(m => m.default || m)`
2018-03-13 14:36:49 +00:00
} else {
2018-03-13 16:41:20 +00:00
return `import _${hash(key)} from '${layouts[key]}'`
2018-03-13 14:36:49 +00:00
}
}).join('\n') %>
2016-12-24 00:55:32 +00:00
2018-10-24 13:46:06 +00:00
const layouts = { <%= Object.keys(layouts).map(key => `"_${key}": _${hash(key)}`).join(',') %> }<%= isTest ? '// eslint-disable-line' : '' %>
2018-03-13 14:36:49 +00:00
<% if (splitChunks.layouts) { %>let resolvedLayouts = {}<% } %>
<% } %>
2016-12-24 00:55:32 +00:00
export default {
<% if (features.meta) { %>
2019-09-10 09:51:14 +00:00
<%= isTest ? '/* eslint-disable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, space-before-function-paren, object-shorthand */' : '' %>
head: <%= serializeFunction(head) %>,
2019-09-10 09:51:14 +00:00
<%= isTest ? '/* eslint-enable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, space-before-function-paren, object-shorthand */' : '' %>
<% } %>
2019-09-10 09:51:14 +00:00
render (h, props) {
<% if (loading) { %>const loadingEl = h('NuxtLoading', { ref: 'loading' })<% } %>
<% if (features.layouts) { %>
2017-11-06 17:30:15 +00:00
const layoutEl = h(this.layout || 'nuxt')
const templateEl = h('div', {
domProps: {
id: '__layout'
},
2017-10-07 09:43:09 +00:00
key: this.layoutName
2017-11-06 17:30:15 +00:00
}, [ layoutEl ])
<% } else { %>
const templateEl = h('nuxt')
<% } %>
2017-10-07 09:43:09 +00:00
<% if (features.transitions) { %>
2017-10-07 09:43:09 +00:00
const transitionEl = h('transition', {
props: {
name: '<%= layoutTransition.name %>',
mode: '<%= layoutTransition.mode %>'
},
on: {
2019-09-10 09:51:14 +00:00
beforeEnter (el) {
// Ensure to trigger scroll event after calling scrollBehavior
window.<%= globals.nuxt %>.$nextTick(() => {
window.<%= globals.nuxt %>.$emit('triggerScroll')
})
}
2017-10-07 09:43:09 +00:00
}
2017-11-06 17:30:15 +00:00
}, [ templateEl ])
<% } %>
2017-10-07 09:43:09 +00:00
2018-10-24 13:46:06 +00:00
return h('div', {
2017-10-07 09:49:30 +00:00
domProps: {
id: '<%= globals.id %>'
2017-10-07 09:49:30 +00:00
}
}, [
<% if (loading) { %>loadingEl, <% } %>
<% if (buildIndicator) { %>h(NuxtBuildIndicator), <% } %>
<% if (features.transitions) { %>transitionEl<% } else { %>templateEl<% } %>
])
2017-10-07 09:43:09 +00:00
},
<% if (features.clientOnline || features.layouts) { %>
2016-12-24 13:15:00 +00:00
data: () => ({
<% if (features.clientOnline) { %>
isOnline: true,
<% } %>
<% if (features.layouts) { %>
2016-12-24 13:15:00 +00:00
layout: null,
layoutName: ''
<% } %>
2016-12-24 13:15:00 +00:00
}),
<% } %>
2019-09-10 09:51:14 +00:00
beforeCreate () {
Vue.util.defineReactive(this, 'nuxt', this.$options.nuxt)
2017-08-14 22:58:45 +00:00
},
2019-09-10 09:51:14 +00:00
created () {
2017-08-14 22:58:45 +00:00
// Add this.$nuxt in child instances
Vue.prototype.<%= globals.nuxt %> = this
2017-08-14 22:58:45 +00:00
// add to window so we can listen when ready
if (process.client) {
window.<%= globals.nuxt %> = <%= (globals.nuxt !== '$nuxt' ? 'window.$nuxt = ' : '') %>this
<% if (features.clientOnline) { %>
this.refreshOnlineStatus()
// Setup the listeners
window.addEventListener('online', this.refreshOnlineStatus)
window.addEventListener('offline', this.refreshOnlineStatus)
<% } %>
2017-08-14 22:58:45 +00:00
}
// Add $nuxt.error()
this.error = this.nuxt.error
// Add $nuxt.context
this.context = this.$options.context
2017-08-14 22:58:45 +00:00
},
<% if (loading) { %>
2019-09-10 09:51:14 +00:00
mounted () {
this.$loading = this.$refs.loading
},
2017-08-14 22:58:45 +00:00
watch: {
'nuxt.err': 'errorChanged'
},
2017-08-14 22:58:45 +00:00
<% } %>
<% if (features.clientOnline) { %>
computed: {
2019-09-10 09:51:14 +00:00
isOffline () {
return !this.isOnline
}
},
<% } %>
2016-12-24 00:55:32 +00:00
methods: {
2019-09-10 09:51:14 +00:00
<%= isTest ? '/* eslint-disable comma-dangle */' : '' %>
<% if (features.clientOnline) { %>
2019-09-10 09:51:14 +00:00
refreshOnlineStatus () {
if (process.client) {
if (typeof window.navigator.onLine === 'undefined') {
// If the browser doesn't support connection status reports
// assume that we are online because most apps' only react
// when they now that the connection has been interrupted
this.isOnline = true
} else {
this.isOnline = window.navigator.onLine
}
}
},
<% } %>
2019-09-10 09:51:14 +00:00
async refresh () {
<% if (features.asyncData || features.fetch) { %>
const pages = getMatchedComponentsInstances(this.$route)
if (!pages.length) {
return
}
<% if (loading) { %>this.$loading.start()<% } %>
2019-09-10 09:51:14 +00:00
const promises = pages.map((page) => {
const p = []
<% if (features.fetch) { %>
if (page.$options.fetch) {
p.push(promisify(page.$options.fetch, this.context))
}
<% } %>
<% if (features.asyncData) { %>
if (page.$options.asyncData) {
p.push(
promisify(page.$options.asyncData, this.context)
.then((newData) => {
for (const key in newData) {
Vue.set(page.$data, key, newData[key])
}
})
)
}
<% } %>
return Promise.all(p)
})
try {
await Promise.all(promises)
} catch (error) {
<% if (loading) { %>this.$loading.fail()<% } %>
globalHandleError(error)
this.error(error)
}
<% if (loading) { %>this.$loading.finish()<% } %>
<% } %>
},
2017-08-14 22:58:45 +00:00
<% if (loading) { %>
2019-09-10 09:51:14 +00:00
errorChanged () {
2017-08-14 22:58:45 +00:00
if (this.nuxt.err && this.$loading) {
2019-09-10 09:51:14 +00:00
if (this.$loading.fail) {
this.$loading.fail()
}
if (this.$loading.finish) {
this.$loading.finish()
}
2017-08-14 22:58:45 +00:00
}
},
<% } %>
<% if (features.layouts) { %>
<% if (splitChunks.layouts) { %>
2019-09-10 09:51:14 +00:00
setLayout (layout) {
<% if (debug) { %>
2019-09-10 09:51:14 +00:00
if(layout && typeof layout !== 'string') {
throw new Error('[nuxt] Avoid using non-string value as layout property.')
}
<% } %>
2019-09-10 09:51:14 +00:00
if (!layout || !resolvedLayouts['_' + layout]) {
layout = 'default'
}
2016-12-24 13:15:00 +00:00
this.layoutName = layout
2016-12-24 11:34:41 +00:00
let _layout = '_' + layout
this.layout = resolvedLayouts[_layout]
2017-02-20 22:11:34 +00:00
return this.layout
2016-12-24 11:34:41 +00:00
},
2019-09-10 09:51:14 +00:00
loadLayout (layout) {
const undef = !layout
2018-12-12 06:31:49 +00:00
const nonexistent = !(layouts['_' + layout] || resolvedLayouts['_' + layout])
let _layout = '_' + ((undef || nonexistent) ? 'default' : layout)
if (resolvedLayouts[_layout]) {
return Promise.resolve(resolvedLayouts[_layout])
2017-02-20 22:11:34 +00:00
}
2016-12-24 11:34:41 +00:00
return layouts[_layout]()
2018-03-13 14:36:49 +00:00
.then((Component) => {
resolvedLayouts[_layout] = Component
delete layouts[_layout]
return resolvedLayouts[_layout]
})
.catch((e) => {
if (this.<%= globals.nuxt %>) {
return this.<%= globals.nuxt %>.error({ statusCode: 500, message: e.message })
2018-03-13 14:36:49 +00:00
}
})
2016-12-24 00:55:32 +00:00
}
2018-03-13 14:36:49 +00:00
<% } else { %>
2019-09-10 09:51:14 +00:00
setLayout (layout) {
<% if (debug) { %>
2019-09-10 09:51:14 +00:00
if(layout && typeof layout !== 'string') {
throw new Error('[nuxt] Avoid using non-string value as layout property.')
}
<% } %>
if (!layout || !layouts['_' + layout]) {
layout = 'default'
}
2018-03-13 14:36:49 +00:00
this.layoutName = layout
this.layout = layouts['_' + layout]
return this.layout
},
2019-09-10 09:51:14 +00:00
loadLayout (layout) {
if (!layout || !layouts['_' + layout]) {
layout = 'default'
}
2018-03-13 14:36:49 +00:00
return Promise.resolve(layouts['_' + layout])
}
<% } /* splitChunks.layouts */ %>
<% } /* features.layouts */ %>
},
<% if (loading) { %>
components: {
NuxtLoading
2016-12-24 00:55:32 +00:00
}
<% } %>
2019-09-10 09:51:14 +00:00
<%= isTest ? '/* eslint-enable comma-dangle */' : '' %>
2016-12-24 00:55:32 +00:00
}