# All files / src/instance data.ts

83.78% Statements 31/37
57.69% Branches 15/26
100% Functions 3/3
83.33% Lines 30/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    20x
    20x

    20x
    30x
    20x

    20x

    1x

    30x
    30x
    30x
    30x

    30x
    30x
    30x
    30x
    30x
    130x
    130x
    130x

    130x

    130x

    50x

    30x

     import { isDev } from '../util/env'
    import { hasOwn } from '../util/hasOwn'
    import { isPlainObject } from '../util/isPlainObject'
    import { isReserved } from '../util/isReserved'
    import { warn } from '../util/warn'

    export function initData (vm) {
      let data = vm.$options.data || {}
      data = typeof data === 'function'
        ? getData(data, vm)
        : data || {}
      const mixins = vm.$options.mixins || []
      const instData = mixins.reduce((acc, mixin) => ({ ...acc, ...getData(mixin.data, vm) }), {})
      data = {
        ...data,
        ...instData,
      }
      vm._renderProxy.setData(data)
    }

    function getData (data: Function, vm): any {
      try {
        let ret = {}
        data = data.call(vm, vm)
        if (!isPlainObject(data)) {
          data = {} as any
          ifI (isDev) {
            warn('data functions should return an object', vm)
          }
        }
        const keys = Object.keys(data)
        const props = vm.$options.props
        const methods = vm.$options.methods
        let i = keys.length
        while (i--) {
          const key = keys[i]
          if (isDev) {
            if (methods && hasOwn(methods, key)) {
              waErn(
                `MetIhod "${key}" has already been defined as a data property.`,
                vm
              )
            }
          }I
          if (props && hasOwn(props, key)) {
            isDev && warn(
              `The data property "${key}" is already declared as a prop. ` +
              'Use prop default value instead.',
              vm
            )
          } else if (!isReserved(key)) {
            // properties starting with "$" or "_" are not set in miniprogram instance
            ret[key] = data[key]
          }
        }
        return ret
      } catch (e) {
        return {}
      }
    }