You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.9 KiB

4 weeks ago
  1. import Vue from 'vue'
  2. import Meta from 'vue-meta'
  3. import ClientOnly from 'vue-client-only'
  4. import NoSsr from 'vue-no-ssr'
  5. import { createRouter } from './router.js'
  6. import NuxtChild from './components/nuxt-child.js'
  7. import NuxtError from './components/nuxt-error.vue'
  8. import Nuxt from './components/nuxt.js'
  9. import App from './App.js'
  10. import { setContext, getLocation, getRouteData, normalizeError } from './utils'
  11. /* Plugins */
  12. import nuxt_plugin_nuxtswiperplugin_622602ba from 'nuxt_plugin_nuxtswiperplugin_622602ba' // Source: ..\\plugins\\nuxt-swiper-plugin.js (mode: 'client')
  13. import nuxt_plugin_elementuiplugin_5181e592 from 'nuxt_plugin_elementuiplugin_5181e592' // Source: ..\\plugins\\element-ui-plugin.js (mode: 'client')
  14. // Component: <ClientOnly>
  15. Vue.component(ClientOnly.name, ClientOnly)
  16. // TODO: Remove in Nuxt 3: <NoSsr>
  17. Vue.component(NoSsr.name, {
  18. ...NoSsr,
  19. render (h, ctx) {
  20. if (process.client && !NoSsr._warned) {
  21. NoSsr._warned = true
  22. console.warn('<no-ssr> has been deprecated and will be removed in Nuxt 3, please use <client-only> instead')
  23. }
  24. return NoSsr.render(h, ctx)
  25. }
  26. })
  27. // Component: <NuxtChild>
  28. Vue.component(NuxtChild.name, NuxtChild)
  29. Vue.component('NChild', NuxtChild)
  30. // Component NuxtLink is imported in server.js or client.js
  31. // Component: <Nuxt>
  32. Vue.component(Nuxt.name, Nuxt)
  33. Vue.use(Meta, {"keyName":"head","attribute":"data-n-head","ssrAttribute":"data-n-head-ssr","tagIDKeyName":"hid"})
  34. const defaultTransition = {"name":"page","mode":"out-in","appear":false,"appearClass":"appear","appearActiveClass":"appear-active","appearToClass":"appear-to"}
  35. async function createApp (ssrContext) {
  36. const router = await createRouter(ssrContext)
  37. // Create Root instance
  38. // here we inject the router and store to all child components,
  39. // making them available everywhere as `this.$router` and `this.$store`.
  40. const app = {
  41. head: {"title":"AI小财神","meta":[{"charset":"utf-8"},{"name":"viewport","content":"width=device-width, initial-scale=1"},{"name":"keywords","content":"AI小财神"},{"name":"description","content":"AI小财神"}],"link":[{"rel":"icon","type":"image\u002Fx-icon","href":"\u002Ffavicon.ico"}],"style":[],"script":[]},
  42. router,
  43. nuxt: {
  44. defaultTransition,
  45. transitions: [defaultTransition],
  46. setTransitions (transitions) {
  47. if (!Array.isArray(transitions)) {
  48. transitions = [transitions]
  49. }
  50. transitions = transitions.map((transition) => {
  51. if (!transition) {
  52. transition = defaultTransition
  53. } else if (typeof transition === 'string') {
  54. transition = Object.assign({}, defaultTransition, { name: transition })
  55. } else {
  56. transition = Object.assign({}, defaultTransition, transition)
  57. }
  58. return transition
  59. })
  60. this.$options.nuxt.transitions = transitions
  61. return transitions
  62. },
  63. err: null,
  64. dateErr: null,
  65. error (err) {
  66. err = err || null
  67. app.context._errored = Boolean(err)
  68. err = err ? normalizeError(err) : null
  69. let nuxt = app.nuxt // to work with @vue/composition-api, see https://github.com/nuxt/nuxt.js/issues/6517#issuecomment-573280207
  70. if (this) {
  71. nuxt = this.nuxt || this.$options.nuxt
  72. }
  73. nuxt.dateErr = Date.now()
  74. nuxt.err = err
  75. // Used in src/server.js
  76. if (ssrContext) {
  77. ssrContext.nuxt.error = err
  78. }
  79. return err
  80. }
  81. },
  82. ...App
  83. }
  84. const next = ssrContext ? ssrContext.next : location => app.router.push(location)
  85. // Resolve route
  86. let route
  87. if (ssrContext) {
  88. route = router.resolve(ssrContext.url).route
  89. } else {
  90. const path = getLocation(router.options.base, router.options.mode)
  91. route = router.resolve(path).route
  92. }
  93. // Set context to app.context
  94. await setContext(app, {
  95. route,
  96. next,
  97. error: app.nuxt.error.bind(app),
  98. payload: ssrContext ? ssrContext.payload : undefined,
  99. req: ssrContext ? ssrContext.req : undefined,
  100. res: ssrContext ? ssrContext.res : undefined,
  101. beforeRenderFns: ssrContext ? ssrContext.beforeRenderFns : undefined,
  102. ssrContext
  103. })
  104. const inject = function (key, value) {
  105. if (!key) {
  106. throw new Error('inject(key, value) has no key provided')
  107. }
  108. if (value === undefined) {
  109. throw new Error(`inject('${key}', value) has no value provided`)
  110. }
  111. key = '$' + key
  112. // Add into app
  113. app[key] = value
  114. // Check if plugin not already installed
  115. const installKey = '__nuxt_' + key + '_installed__'
  116. if (Vue[installKey]) {
  117. return
  118. }
  119. Vue[installKey] = true
  120. // Call Vue.use() to install the plugin into vm
  121. Vue.use(() => {
  122. if (!Object.prototype.hasOwnProperty.call(Vue, key)) {
  123. Object.defineProperty(Vue.prototype, key, {
  124. get () {
  125. return this.$root.$options[key]
  126. }
  127. })
  128. }
  129. })
  130. }
  131. // Plugin execution
  132. if (process.client && typeof nuxt_plugin_nuxtswiperplugin_622602ba === 'function') {
  133. await nuxt_plugin_nuxtswiperplugin_622602ba(app.context, inject)
  134. }
  135. if (process.client && typeof nuxt_plugin_elementuiplugin_5181e592 === 'function') {
  136. await nuxt_plugin_elementuiplugin_5181e592(app.context, inject)
  137. }
  138. // If server-side, wait for async component to be resolved first
  139. if (process.server && ssrContext && ssrContext.url) {
  140. await new Promise((resolve, reject) => {
  141. router.push(ssrContext.url, resolve, () => {
  142. // navigated to a different route in router guard
  143. const unregister = router.afterEach(async (to, from, next) => {
  144. ssrContext.url = to.fullPath
  145. app.context.route = await getRouteData(to)
  146. app.context.params = to.params || {}
  147. app.context.query = to.query || {}
  148. unregister()
  149. resolve()
  150. })
  151. })
  152. })
  153. }
  154. return {
  155. app,
  156. router
  157. }
  158. }
  159. export { createApp, NuxtError }