保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import Vue from 'vue'
import { hasFetch, normalizeError, addLifecycleHook, createGetCounter } from '../utils'
 
const isSsrHydration = (vm) => vm.$vnode && vm.$vnode.elm && vm.$vnode.elm.dataset && vm.$vnode.elm.dataset.fetchKey
const nuxtState = window.<%= globals.context %>
 
export default {
  beforeCreate () {
    if (!hasFetch(this)) {
      return
    }
 
    this._fetchDelay = typeof this.$options.fetchDelay === 'number' ? this.$options.fetchDelay : 200
 
    Vue.util.defineReactive(this, '$fetchState', {
      pending: false,
      error: null,
      timestamp: Date.now()
    })
 
    this.$fetch = $fetch.bind(this)
    addLifecycleHook(this, 'created', created)
    addLifecycleHook(this, 'beforeMount', beforeMount)
  }
}
 
function beforeMount() {
  if (!this._hydrated) {
    return this.$fetch()
  }
}
 
function created() {
  if (!isSsrHydration(this)) {
    <% if (isFullStatic) { %>createdFullStatic.call(this)<% } %>
    return
  }
 
  // Hydrate component
  this._hydrated = true
  this._fetchKey = this.$vnode.elm.dataset.fetchKey
  const data = nuxtState.fetch[this._fetchKey]
 
  // If fetch error
  if (data && data._error) {
    this.$fetchState.error = data._error
    return
  }
 
  // Merge data
  for (const key in data) {
    Vue.set(this.$data, key, data[key])
  }
}
 
<% if (isFullStatic) { %>
function createdFullStatic() {
  // Check if component has been fetched on server
  let fetchedOnServer = this.$options.fetchOnServer !== false
  if (typeof this.$options.fetchOnServer === 'function') {
    fetchedOnServer = this.$options.fetchOnServer.call(this) !== false
  }
  if (!fetchedOnServer || this.<%= globals.nuxt %>.isPreview || !this.<%= globals.nuxt %>._pagePayload) {
    return
  }
  this._hydrated = true
 
  const defaultKey = this.$options._scopeId || this.$options.name || ''
  const getCounter = createGetCounter(this.<%= globals.nuxt %>._fetchCounters, defaultKey)
 
  if (typeof this.$options.fetchKey === 'function') {
    this._fetchKey = this.$options.fetchKey.call(this, getCounter)
  } else {
    const key = 'string' === typeof this.$options.fetchKey ? this.$options.fetchKey : defaultKey
    this._fetchKey = key ? key + ':' + getCounter(key) : String(getCounter(key))
  }
 
  const data = this.<%= globals.nuxt %>._pagePayload.fetch[this._fetchKey]
 
  // If fetch error
  if (data && data._error) {
    this.$fetchState.error = data._error
    return
  }
 
  // If there is a missing payload
  if (!data) {
    this.$fetch()
    return
  }
 
  // Merge data
  for (const key in data) {
    Vue.set(this.$data, key, data[key])
  }
}
<% } %>
 
function $fetch() {
  if (!this._fetchPromise) {
    this._fetchPromise = $_fetch.call(this)
      .then(() => { delete this._fetchPromise })
  }
  return this._fetchPromise
}
 
async function $_fetch() {
  this.<%= globals.nuxt %>.nbFetching++
  this.$fetchState.pending = true
  this.$fetchState.error = null
  this._hydrated = false
  let error = null
  const startTime = Date.now()
 
  try {
    await this.$options.fetch.call(this)
  } catch (err) {
    if (process.dev) {
      console.error('Error in fetch():', err)
    }
    error = normalizeError(err)
  }
 
  const delayLeft = this._fetchDelay - (Date.now() - startTime)
  if (delayLeft > 0) {
    await new Promise(resolve => setTimeout(resolve, delayLeft))
  }
 
  this.$fetchState.error = error
  this.$fetchState.pending = false
  this.$fetchState.timestamp = Date.now()
 
  this.$nextTick(() => this.<%= globals.nuxt %>.nbFetching--)
}