UNPKG

4.46 kBJavaScriptView Raw
1import Vue from 'vue'
2<% if (loading) { %>import NuxtLoading from '<%= (typeof loading === "string" ? loading : "./components/nuxt-loading.vue") %>'<% } %>
3<% css.forEach((c) => { %>
4import '<%= relativeToBuild(resolvePath(c.src || c)) %>'
5<% }) %>
6
7<%= Object.keys(layouts).map((key) => {
8 if (splitChunks.layouts) {
9 return `const _${hash(key)} = () => import('${layouts[key]}' /* webpackChunkName: "${wChunk('layouts/' + key)}" */).then(m => m.default || m)`
10 } else {
11 return `import _${hash(key)} from '${layouts[key]}'`
12 }
13}).join('\n') %>
14
15const layouts = { <%= Object.keys(layouts).map(key => `"_${key}": _${hash(key)}`).join(',') %> }<%= isTest ? '// eslint-disable-line' : '' %>
16
17<% if (splitChunks.layouts) { %>let resolvedLayouts = {}<% } %>
18
19export default {
20 <%= isTest ? '/* eslint-disable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, object-property-newline, arrow-parens */' : '' %>
21 head: <%= serialize(head).replace(/:\w+\(/gm, ':function(') %>,
22 <%= isTest ? '/* eslint-enable quotes, semi, indent, comma-spacing, key-spacing, object-curly-spacing, object-property-newline, arrow-parens */' : '' %>
23 render(h, props) {
24 <% if (loading) { %>const loadingEl = h('nuxt-loading', { ref: 'loading' })<% } %>
25 const layoutEl = h(this.layout || 'nuxt')
26 const templateEl = h('div', {
27 domProps: {
28 id: '__layout'
29 },
30 key: this.layoutName
31 }, [ layoutEl ])
32
33 const transitionEl = h('transition', {
34 props: {
35 name: '<%= layoutTransition.name %>',
36 mode: '<%= layoutTransition.mode %>'
37 }
38 }, [ templateEl ])
39
40 return h('div', {
41 domProps: {
42 id: '<%= globals.id %>'
43 }
44 }, [
45 <% if (loading) { %>loadingEl,<% } %>
46 transitionEl
47 ])
48 },
49 data: () => ({
50 layout: null,
51 layoutName: ''
52 }),
53 beforeCreate() {
54 Vue.util.defineReactive(this, 'nuxt', this.$options.nuxt)
55 },
56 created() {
57 // Add this.$nuxt in child instances
58 Vue.prototype.<%= globals.nuxt %> = this
59 // add to window so we can listen when ready
60 if (typeof window !== 'undefined') {
61 window.<%= globals.nuxt %> = this
62 <% if (globals.nuxt !== '$nuxt') { %>
63 window.$nuxt = true
64 <% } %>
65 }
66 // Add $nuxt.error()
67 this.error = this.nuxt.error
68 },
69 <% if (loading) { %>
70 mounted() {
71 this.$loading = this.$refs.loading
72 },
73 watch: {
74 'nuxt.err': 'errorChanged'
75 },
76 <% } %>
77 methods: {
78 <% if (loading) { %>
79 errorChanged() {
80 if (this.nuxt.err && this.$loading) {
81 if (this.$loading.fail) this.$loading.fail()
82 if (this.$loading.finish) this.$loading.finish()
83 }
84 },
85 <% } %>
86 <% if (splitChunks.layouts) { %>
87 setLayout(layout) {
88 <% if (debug) { %>
89 if(layout && typeof layout !== 'string') throw new Error('[nuxt] Avoid using non-string value as layout property.')
90 <% } %>
91 if (!layout || !resolvedLayouts['_' + layout]) layout = 'default'
92 this.layoutName = layout
93 let _layout = '_' + layout
94 this.layout = resolvedLayouts[_layout]
95 return this.layout
96 },
97 loadLayout(layout) {
98 const undef = !layout
99 const inexisting = !(layouts['_' + layout] || resolvedLayouts['_' + layout])
100 let _layout = '_' + ((undef || inexisting) ? 'default' : layout)
101 if (resolvedLayouts[_layout]) {
102 return Promise.resolve(resolvedLayouts[_layout])
103 }
104 return layouts[_layout]()
105 .then((Component) => {
106 resolvedLayouts[_layout] = Component
107 delete layouts[_layout]
108 return resolvedLayouts[_layout]
109 })
110 .catch((e) => {
111 if (this.<%= globals.nuxt %>) {
112 return this.<%= globals.nuxt %>.error({ statusCode: 500, message: e.message })
113 }
114 })
115 }
116 <% } else { %>
117 setLayout(layout) {
118 <% if (debug) { %>
119 if(layout && typeof layout !== 'string') throw new Error('[nuxt] Avoid using non-string value as layout property.')
120 <% } %>
121 if (!layout || !layouts['_' + layout]) {
122 layout = 'default'
123 }
124 this.layoutName = layout
125 this.layout = layouts['_' + layout]
126 return this.layout
127 },
128 loadLayout(layout) {
129 if (!layout || !layouts['_' + layout]) {
130 layout = 'default'
131 }
132 return Promise.resolve(layouts['_' + layout])
133 }
134 <% } %>
135 },
136 components: {
137 <%= (loading ? 'NuxtLoading' : '') %>
138 }
139}
140
\No newline at end of file