Nuxt/packages/vue-app/template/components/nuxt.js

91 lines
2.3 KiB
JavaScript
Raw Normal View History

import Vue from 'vue'
2018-10-24 13:46:06 +00:00
import { compile } from '../utils'
<% if (components.ErrorPage) { %>
<% if (('~@').includes(components.ErrorPage.charAt(0))) { %>
import NuxtError from '<%= components.ErrorPage %>'
<% } else { %>
import NuxtError from '<%= "../" + components.ErrorPage %>'
<% } %>
<% } else { %>
import NuxtError from './nuxt-error.vue'
<% } %>
2018-10-24 13:46:06 +00:00
import NuxtChild from './nuxt-child'
2019-01-06 07:56:59 +00:00
<%= isTest ? '// @vue/component' : '' %>
export default {
name: 'Nuxt',
2019-01-06 07:56:59 +00:00
components: {
NuxtChild,
NuxtError
},
2018-10-24 13:46:06 +00:00
props: {
2019-01-06 07:56:59 +00:00
nuxtChildKey: {
type: String,
default: undefined
},
keepAlive: Boolean,
2019-01-06 07:56:59 +00:00
keepAliveProps: {
type: Object,
default: undefined
},
name: {
type: String,
default: 'default'
}
2018-10-24 13:46:06 +00:00
},
2019-01-06 07:56:59 +00:00
computed: {
routerViewKey() {
// If nuxtChildKey prop is given or current route has children
if (typeof this.nuxtChildKey !== 'undefined' || this.$route.matched.length > 1) {
return this.nuxtChildKey || compile(this.$route.matched[0].path)(this.$route.params)
}
const [matchedRoute] = this.$route.matched
const Component = matchedRoute && matchedRoute.components.default
if (Component && Component.options) {
const { key, watchQuery } = Component.options
if (key) {
return (typeof key === 'function' ? key(this.$route) : key)
}
if (watchQuery) {
if (watchQuery.length) {
const pickedQuery = {}
for (const queryKey of watchQuery) {
pickedQuery[queryKey] = this.$route.query[queryKey]
}
return this.$router.resolve({
path: this.$route.path,
query: pickedQuery
}).href
}
return this.$route.fullPath
}
2019-01-06 07:56:59 +00:00
}
const strict = /\/$/.test(matchedRoute.path)
return strict ? this.$route.path : this.$route.path.replace(/\/$/, '')
2019-01-06 07:56:59 +00:00
}
},
beforeCreate() {
Vue.util.defineReactive(this, 'nuxt', this.$root.$options.nuxt)
},
2017-10-07 09:47:31 +00:00
render(h) {
// If there is some error
if (this.nuxt.err) {
return h('NuxtError', {
2017-10-07 09:47:31 +00:00
props: {
error: this.nuxt.err
}
})
}
// Directly return nuxt child
return h('NuxtChild', {
key: this.routerViewKey,
props: this.$props
2017-10-07 09:47:31 +00:00
})
}
}