get-app.uvue 1.9 KB
Newer Older
1 2 3 4
<template>
  <view>
    <page-head title="getApp"></page-head>
    <view class="uni-padding-wrap">
5 6 7 8 9 10
      <button @click="getGlobalData">get globalData</button>
      <button @click="setGlobalData"  class="uni-common-mt">set globalData</button>
      <template v-if="globalDataStr">
        <text class="uni-common-mt">globalData str: {{ globalDataStr }}</text>
        <text class="uni-common-mt">globalData num: {{ globalDataNum }}</text>
        <text class="uni-common-mt">globalData boolean: {{ globalDataBool }}</text>
11
      </template>
12
      <text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
      <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>
</template>

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

  export default {
    data() {
      return {
        globalDataStr: '',
29
        globalDataNum: 0,
30 31 32 33 34 35 36 37 38 39 40
        globalDataBool: false,
        lifeCycleNum: 0,
      }
    },
    onReady() {
      this.lifeCycleNum = state.lifeCycleNum
    },
    methods: {
      getGlobalData() {
        const app = getApp()
        this.globalDataStr = app.globalData.str
41
        this.globalDataNum = app.globalData.num
42 43
        this.globalDataBool = app.globalData.bool
      },
44 45 46 47 48 49
      setGlobalData() {
        const app = getApp()
        app.globalData.str = 'new global data str'
        app.globalData.num = 456
        app.globalData.bool = false
      },
50 51 52 53 54 55 56 57 58 59 60
      _increasetLifeCycleNum: function () {
        const app = getApp()
        app.increasetLifeCycleNum()
        this.lifeCycleNum = state.lifeCycleNum
      },
      // 自动化测试
      setLifeCycleNum(num : number) {
        setLifeCycleNum(num)
      }
    },
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
61
</script>