get-app.uvue 6.3 KB
Newer Older
雪洛's avatar
雪洛 已提交
1 2 3 4 5 6 7
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1;padding-bottom: 20px;">
  <!-- #endif -->
    <view>
      <page-head title="getApp"></page-head>
      <view class="uni-padding-wrap">
雪洛's avatar
雪洛 已提交
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
        <button @click="getGlobalData">get globalData</button>
        <template v-if="originGlobalData.str.length">
          <text class="uni-common-mt bold">初始的 globalData:</text>
          <text class="uni-common-mt">globalData string: {{ originGlobalData.str }}</text>
          <text class="uni-common-mt">globalData number: {{ originGlobalData.num }}</text>
          <text class="uni-common-mt">globalData boolean: {{ originGlobalData.bool }}</text>
          <text class="uni-common-mt">globalData object: {{ originGlobalData.obj }}</text>
          <text class="uni-common-mt">globalData null: {{ originGlobalData.null }}</text>
          <text class="uni-common-mt">globalData array: {{ originGlobalData.arr }}</text>
          <text class="uni-common-mt">globalData Set: {{ originGlobalData.mySet }}</text>
          <text class="uni-common-mt">globalData Map: {{ originGlobalData.myMap }}</text>
          <text class="uni-common-mt">globalData func 返回值: {{ originGlobalDataFuncRes }}</text>
        </template>
        <button @click="setGlobalData" class="uni-common-mt">set globalData</button>
        <template v-if="newGlobalData.bool">
          <text class="uni-common-mt bold">更新后的 globalData:</text>
          <text class="uni-common-mt">globalData string: {{ newGlobalData.str }}</text>
          <text class="uni-common-mt">globalData number: {{ newGlobalData.num }}</text>
          <text class="uni-common-mt">globalData boolean: {{ newGlobalData.bool }}</text>
          <text class="uni-common-mt">globalData object: {{ newGlobalData.obj }}</text>
          <text class="uni-common-mt">globalData null: {{ newGlobalData.null }}</text>
          <text class="uni-common-mt">globalData array: {{ newGlobalData.arr }}</text>
          <text class="uni-common-mt">globalData Set: {{ newGlobalData.mySet }}</text>
          <text class="uni-common-mt">globalData Map: {{ newGlobalData.myMap }}</text>
          <text class="uni-common-mt">globalData func 返回值: {{ newGlobalDataFuncRes }}</text>
        </template>
雪洛's avatar
雪洛 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        <text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
        <text class="margin-top:6px;">increasetLifeCycleNum 方法</text>
        <button class="uni-common-mt" @click="_increasetLifeCycleNum">
          increase lifeCycleNum
        </button>
        <text class="uni-common-mt">lifeCycleNum: {{ lifeCycleNum }}</text>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts">
  import { state, setLifeCycleNum } from '@/store/index.uts'

雪洛's avatar
雪洛 已提交
50 51 52 53 54 55 56 57 58 59 60 61
  type MyGlobalData = {
    str : string,
    num : number,
    bool : boolean,
    obj : UTSJSONObject,
    null : string | null,
    arr : number[],
    mySet : string[],
    myMap : UTSJSONObject,
    func : () => string
  }

雪洛's avatar
雪洛 已提交
62 63 64
  export default {
    data() {
      return {
雪洛's avatar
雪洛 已提交
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
        originGlobalData: {
          str: '',
          num: 0,
          bool: false,
          obj: {
            str: '',
            num: 0,
            bool: false
          } as UTSJSONObject,
          null: null,
          arr: [] as number[],
          mySet: [] as string[],
          myMap: {},
          func: () : string => ''
        } as MyGlobalData,
        originGlobalDataFuncRes: '',
        newGlobalData: {
          str: '',
          num: 0,
          bool: false,
          obj: {
            str: '',
            num: 0,
            bool: false
          } as UTSJSONObject,
          null: null,
          arr: [] as number[],
          mySet: [] as string[],
          myMap: {},
          func: () : string => ''
        } as MyGlobalData,
        newGlobalDataFuncRes: '',
雪洛's avatar
雪洛 已提交
97 98 99 100 101 102 103
        lifeCycleNum: 0,
      }
    },
    onReady() {
      this.lifeCycleNum = state.lifeCycleNum
    },
    methods: {
雪洛's avatar
雪洛 已提交
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      getGlobalData() {
        const app = getApp()

        this.originGlobalData.str = app.globalData.str
        this.originGlobalData.num = app.globalData.num
        this.originGlobalData.bool = app.globalData.bool
        this.originGlobalData.obj = app.globalData.obj
        this.originGlobalData.null = app.globalData.null
        this.originGlobalData.arr = app.globalData.arr
        app.globalData.mySet.forEach((value : string) => {
          this.originGlobalData.mySet.push(value)
        })
        app.globalData.myMap.forEach((value : any, key : string) => {
          this.originGlobalData.myMap[key] = value
        })
        this.originGlobalData.func = app.globalData.func
        this.originGlobalDataFuncRes = this.originGlobalData.func()
      },
      setGlobalData() {
        const app = getApp()

        app.globalData.str = 'new globalData str'
        app.globalData.num = 100
        app.globalData.bool = true
        app.globalData.obj = {
          str: 'new globalData obj str',
          num: 200,
          bool: true
        }
        app.globalData.null = 'not null'
        app.globalData.arr = [1, 2, 3]
        app.globalData.mySet = new Set(['a', 'b', 'c'])
        app.globalData.myMap = new Map([
          ['a', 1],
          ['b', 2],
          ['c', 3]
        ])
        app.globalData.func = () : string => {
          return 'new globalData func'
        }

        this.newGlobalData.str = app.globalData.str
        this.newGlobalData.num = app.globalData.num
        this.newGlobalData.bool = app.globalData.bool
        this.newGlobalData.obj = app.globalData.obj
        this.newGlobalData.null = app.globalData.null
        this.newGlobalData.arr = app.globalData.arr
        app.globalData.mySet.forEach((value : string) => {
          this.newGlobalData.mySet.push(value)
        })
        app.globalData.myMap.forEach((value : any, key : string) => {
          this.newGlobalData.myMap[key] = value
        })
        this.newGlobalData.func = app.globalData.func
        this.newGlobalDataFuncRes = this.newGlobalData.func()
      },
雪洛's avatar
雪洛 已提交
160 161 162 163 164 165 166 167 168 169
      _increasetLifeCycleNum: function () {
        const app = getApp()
        app.increasetLifeCycleNum()
        this.lifeCycleNum = state.lifeCycleNum
      },
      // 自动化测试
      setLifeCycleNum(num : number) {
        setLifeCycleNum(num)
      }
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
170
  }
雪洛's avatar
雪洛 已提交
171 172 173 174 175 176
</script>

<style>
  .bold {
    font-weight: bold;
  }
雪洛's avatar
雪洛 已提交
177 178 179 180

  .hr {
    border-bottom: 1px solid #ccc;
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
181
</style>