App.js 4.7 KB

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