get-app.uvue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1; padding-bottom: 20px">
  4. <!-- #endif -->
  5. <view>
  6. <page-head title="getApp"></page-head>
  7. <view class="uni-padding-wrap">
  8. <button @click="getGlobalData">get globalData</button>
  9. <template v-if="originGlobalData.str.length">
  10. <text class="uni-common-mt bold">初始的 globalData:</text>
  11. <text class="uni-common-mt">globalData string: {{ originGlobalData.str }}</text>
  12. <text class="uni-common-mt">globalData number: {{ originGlobalData.num }}</text>
  13. <text class="uni-common-mt">globalData boolean: {{ originGlobalData.bool }}</text>
  14. <text class="uni-common-mt">globalData object: {{ originGlobalData.obj }}</text>
  15. <text class="uni-common-mt">globalData null: {{ originGlobalData.null }}</text>
  16. <text class="uni-common-mt">globalData array: {{ originGlobalData.arr }}</text>
  17. <text class="uni-common-mt">globalData Set: {{ originGlobalData.mySet }}</text>
  18. <text class="uni-common-mt">globalData Map: {{ originGlobalData.myMap }}</text>
  19. <text class="uni-common-mt">globalData func 返回值: {{ originGlobalDataFuncRes }}</text>
  20. </template>
  21. <button @click="setGlobalData" class="uni-common-mt">
  22. set globalData
  23. </button>
  24. <template v-if="newGlobalData.bool">
  25. <text class="uni-common-mt bold">更新后的 globalData:</text>
  26. <text class="uni-common-mt">globalData string: {{ newGlobalData.str }}</text>
  27. <text class="uni-common-mt">globalData number: {{ newGlobalData.num }}</text>
  28. <text class="uni-common-mt">globalData boolean: {{ newGlobalData.bool }}</text>
  29. <text class="uni-common-mt">globalData object: {{ newGlobalData.obj }}</text>
  30. <text class="uni-common-mt">globalData null: {{ newGlobalData.null }}</text>
  31. <text class="uni-common-mt">globalData array: {{ newGlobalData.arr }}</text>
  32. <text class="uni-common-mt">globalData Set: {{ newGlobalData.mySet }}</text>
  33. <text class="uni-common-mt">globalData Map: {{ newGlobalData.myMap }}</text>
  34. <text class="uni-common-mt">globalData func 返回值: {{ newGlobalDataFuncRes }}</text>
  35. </template>
  36. <text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
  37. <text class="uni-common-mt">increasetLifeCycleNum 方法</text>
  38. <button class="uni-common-mt" @click="_increasetLifeCycleNum">
  39. increase lifeCycleNum
  40. </button>
  41. <text class="uni-common-mt">lifeCycleNum: {{ lifeCycleNum }}</text>
  42. <!-- #ifndef MP -->
  43. <button class="uni-common-mt" @click="getAndroidApplication">
  44. getAndroidApplication
  45. </button>
  46. <text class="uni-common-mt">androidApplication is null: {{ androidApplication == null }}</text>
  47. <!-- #endif -->
  48. </view>
  49. </view>
  50. <!-- #ifdef APP -->
  51. </scroll-view>
  52. <!-- #endif -->
  53. </template>
  54. <script lang="uts">
  55. import { state, setLifeCycleNum } from '@/store/index.uts'
  56. type MyGlobalData = {
  57. str : string,
  58. num : number,
  59. bool : boolean,
  60. obj : UTSJSONObject,
  61. null : string | null,
  62. arr : number[],
  63. mySet : string[],
  64. myMap : UTSJSONObject,
  65. func : () => string
  66. }
  67. export default {
  68. data() {
  69. return {
  70. originGlobalData: {
  71. str: '',
  72. num: 0,
  73. bool: false,
  74. obj: {
  75. str: '',
  76. num: 0,
  77. bool: false
  78. } as UTSJSONObject,
  79. null: null,
  80. arr: [] as number[],
  81. mySet: [] as string[],
  82. myMap: {},
  83. func: () : string => ''
  84. } as MyGlobalData,
  85. originGlobalDataFuncRes: '',
  86. newGlobalData: {
  87. str: '',
  88. num: 0,
  89. bool: false,
  90. obj: {
  91. str: '',
  92. num: 0,
  93. bool: false
  94. } as UTSJSONObject,
  95. null: null,
  96. arr: [] as number[],
  97. mySet: [] as string[],
  98. myMap: {},
  99. func: () : string => ''
  100. } as MyGlobalData,
  101. newGlobalDataFuncRes: '',
  102. lifeCycleNum: 0,
  103. androidApplication: null as any | null
  104. }
  105. },
  106. onReady() {
  107. this.lifeCycleNum = state.lifeCycleNum
  108. },
  109. methods: {
  110. getGlobalData() {
  111. const app = getApp()
  112. this.originGlobalData.str = app.globalData.str
  113. this.originGlobalData.num = app.globalData.num
  114. this.originGlobalData.bool = app.globalData.bool
  115. this.originGlobalData.obj = app.globalData.obj
  116. this.originGlobalData.null = app.globalData.null
  117. this.originGlobalData.arr = app.globalData.arr
  118. app.globalData.mySet.forEach((value : string) => {
  119. this.originGlobalData.mySet.push(value)
  120. })
  121. app.globalData.myMap.forEach((value : any, key : string) => {
  122. this.originGlobalData.myMap[key] = value
  123. })
  124. this.originGlobalData.func = app.globalData.func
  125. this.originGlobalDataFuncRes = this.originGlobalData.func()
  126. },
  127. setGlobalData() {
  128. const app = getApp()
  129. app.globalData.str = 'new globalData str'
  130. app.globalData.num = 100
  131. app.globalData.bool = true
  132. app.globalData.obj = {
  133. str: 'new globalData obj str',
  134. num: 200,
  135. bool: true
  136. }
  137. app.globalData.null = 'not null'
  138. app.globalData.arr = [1, 2, 3]
  139. app.globalData.mySet = new Set(['a', 'b', 'c'])
  140. app.globalData.myMap = new Map([
  141. ['a', 1],
  142. ['b', 2],
  143. ['c', 3]
  144. ])
  145. app.globalData.func = () : string => {
  146. return 'new globalData func'
  147. }
  148. this.newGlobalData.str = app.globalData.str
  149. this.newGlobalData.num = app.globalData.num
  150. this.newGlobalData.bool = app.globalData.bool
  151. this.newGlobalData.obj = app.globalData.obj
  152. this.newGlobalData.null = app.globalData.null
  153. this.newGlobalData.arr = app.globalData.arr
  154. app.globalData.mySet.forEach((value : string) => {
  155. this.newGlobalData.mySet.push(value)
  156. })
  157. app.globalData.myMap.forEach((value : any, key : string) => {
  158. this.newGlobalData.myMap[key] = value
  159. })
  160. this.newGlobalData.func = app.globalData.func
  161. this.newGlobalDataFuncRes = this.newGlobalData.func()
  162. },
  163. _increasetLifeCycleNum: function () {
  164. const app = getApp()
  165. app.vm!.increasetLifeCycleNum()
  166. this.lifeCycleNum = state.lifeCycleNum
  167. },
  168. // 自动化测试
  169. setLifeCycleNum(num : number) {
  170. setLifeCycleNum(num)
  171. },
  172. // #ifndef MP
  173. getAndroidApplication() : boolean {
  174. const app = getApp()
  175. this.androidApplication = app.getAndroidApplication()
  176. return this.androidApplication !== null
  177. }
  178. // #endif
  179. },
  180. }
  181. </script>
  182. <style>
  183. .bold {
  184. font-weight: bold;
  185. }
  186. .hr {
  187. border-bottom: 1px solid #ccc;
  188. }
  189. </style>