App.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import Vue from 'vue'
  2. import { getMatchedComponentsInstances, getChildrenComponentInstancesUsingFetch, promisify, globalHandleError, urlJoin, sanitizeComponent } from './utils'
  3. import NuxtLoading from './components/nuxt-loading.vue'
  4. import '..\\assets\\css\\common.css'
  5. import '..\\assets\\css\\special.css'
  6. import '..\\node_modules\\swiper\\dist\\css\\swiper.css'
  7. import _6f6c098b from '..\\layouts\\default.vue'
  8. import _bd0fdcf8 from '..\\layouts\\noheader.vue'
  9. import _42906ebb from '..\\layouts\\opacity_header_kf_tmp.vue'
  10. import _77223ab7 from '..\\layouts\\opacity_header.vue'
  11. const layouts = { "_default": sanitizeComponent(_6f6c098b),"_noheader": sanitizeComponent(_bd0fdcf8),"_opacity_header_kf_tmp": sanitizeComponent(_42906ebb),"_opacity_header": sanitizeComponent(_77223ab7) }
  12. export default {
  13. render (h, props) {
  14. const loadingEl = h('NuxtLoading', { ref: 'loading' })
  15. const layoutEl = h(this.layout || 'nuxt')
  16. const templateEl = h('div', {
  17. domProps: {
  18. id: '__layout'
  19. },
  20. key: this.layoutName
  21. }, [layoutEl])
  22. const transitionEl = h('transition', {
  23. props: {
  24. name: 'layout',
  25. mode: 'out-in'
  26. },
  27. on: {
  28. beforeEnter (el) {
  29. // Ensure to trigger scroll event after calling scrollBehavior
  30. window.$nuxt.$nextTick(() => {
  31. window.$nuxt.$emit('triggerScroll')
  32. })
  33. }
  34. }
  35. }, [templateEl])
  36. return h('div', {
  37. domProps: {
  38. id: '__nuxt'
  39. }
  40. }, [
  41. loadingEl,
  42. transitionEl
  43. ])
  44. },
  45. data: () => ({
  46. isOnline: true,
  47. layout: null,
  48. layoutName: '',
  49. nbFetching: 0
  50. }),
  51. beforeCreate () {
  52. Vue.util.defineReactive(this, 'nuxt', this.$options.nuxt)
  53. },
  54. created () {
  55. // Add this.$nuxt in child instances
  56. Vue.prototype.$nuxt = this
  57. if (process.client) {
  58. // add to window so we can listen when ready
  59. window.$nuxt = this
  60. this.refreshOnlineStatus()
  61. // Setup the listeners
  62. window.addEventListener('online', this.refreshOnlineStatus)
  63. window.addEventListener('offline', this.refreshOnlineStatus)
  64. }
  65. // Add $nuxt.error()
  66. this.error = this.nuxt.error
  67. // Add $nuxt.context
  68. this.context = this.$options.context
  69. },
  70. async mounted () {
  71. this.$loading = this.$refs.loading
  72. },
  73. watch: {
  74. 'nuxt.err': 'errorChanged'
  75. },
  76. computed: {
  77. isOffline () {
  78. return !this.isOnline
  79. },
  80. isFetching () {
  81. return this.nbFetching > 0
  82. },
  83. },
  84. methods: {
  85. refreshOnlineStatus () {
  86. if (process.client) {
  87. if (typeof window.navigator.onLine === 'undefined') {
  88. // If the browser doesn't support connection status reports
  89. // assume that we are online because most apps' only react
  90. // when they now that the connection has been interrupted
  91. this.isOnline = true
  92. } else {
  93. this.isOnline = window.navigator.onLine
  94. }
  95. }
  96. },
  97. async refresh () {
  98. const pages = getMatchedComponentsInstances(this.$route)
  99. if (!pages.length) {
  100. return
  101. }
  102. this.$loading.start()
  103. const promises = pages.map((page) => {
  104. const p = []
  105. // Old fetch
  106. if (page.$options.fetch && page.$options.fetch.length) {
  107. p.push(promisify(page.$options.fetch, this.context))
  108. }
  109. if (page.$fetch) {
  110. p.push(page.$fetch())
  111. } else {
  112. // Get all component instance to call $fetch
  113. for (const component of getChildrenComponentInstancesUsingFetch(page.$vnode.componentInstance)) {
  114. p.push(component.$fetch())
  115. }
  116. }
  117. if (page.$options.asyncData) {
  118. p.push(
  119. promisify(page.$options.asyncData, this.context)
  120. .then((newData) => {
  121. for (const key in newData) {
  122. Vue.set(page.$data, key, newData[key])
  123. }
  124. })
  125. )
  126. }
  127. return Promise.all(p)
  128. })
  129. try {
  130. await Promise.all(promises)
  131. } catch (error) {
  132. this.$loading.fail(error)
  133. globalHandleError(error)
  134. this.error(error)
  135. }
  136. this.$loading.finish()
  137. },
  138. errorChanged () {
  139. if (this.nuxt.err && this.$loading) {
  140. if (this.$loading.fail) {
  141. this.$loading.fail(this.nuxt.err)
  142. }
  143. if (this.$loading.finish) {
  144. this.$loading.finish()
  145. }
  146. }
  147. },
  148. setLayout (layout) {
  149. if (!layout || !layouts['_' + layout]) {
  150. layout = 'default'
  151. }
  152. this.layoutName = layout
  153. this.layout = layouts['_' + layout]
  154. return this.layout
  155. },
  156. loadLayout (layout) {
  157. if (!layout || !layouts['_' + layout]) {
  158. layout = 'default'
  159. }
  160. return Promise.resolve(layouts['_' + layout])
  161. },
  162. },
  163. components: {
  164. NuxtLoading
  165. }
  166. }