page-lifecycle.uvue 4.4 KB
Newer Older
1 2
<template>
  <!-- #ifdef APP -->
3
  <scroll-view style="flex:1" :bounces="false">
4 5
  <!-- #endif -->
    <view class="page container">
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
      <view class="item-container">
        <text>onLoad 触发:</text>
        <text>{{ isOnloadTriggered }}</text>
      </view>
      <view class="item-container">
        <text>onShow 触发:</text>
        <text>{{ isOnShowTriggered }}</text>
      </view>
      <view class="item-container">
        <text>onReady 触发:</text>
        <text>{{ isOnReadyTriggered }}</text>
      </view>
      <view class="item-container">
        <text>onPullDownRefresh 触发:</text>
        <text>{{ isOnPullDownRefreshTriggered }}</text>
      </view>
      <view class="item-container">
        <text>onReachBottom 触发:</text>
        <text>{{ isOnReachBottomTriggered }}</text>
      </view>
      <view class="item-container">
        <text>onBackPress 触发:</text>
        <text>{{ isOnBackPressTriggered }}</text>
      </view>
      <view class="item-container">
        <text>onHide 触发:</text>
        <text>{{ isOnHideTriggered }}</text>
      </view>
      <view class="item-container">
        <text>onResize 触发:</text>
        <text>{{ isOnResizeTriggered }}</text>
      </view>
38
      <button class="uni-common-mt" @click="scrollToBottom">scrollToBottom</button>
39 40 41
      <button class="uni-common-mt" @click="pullDownRefresh">
        trigger pullDownRefresh
      </button>
42 43 44 45 46 47
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

48
<script setup lang='uts'>
49 50 51
  import { state, setLifeCycleNum } from '@/store/index.uts'
  
  const isScrolled = ref(false)
52 53 54 55 56 57 58 59 60
  const isOnloadTriggered = ref(false)
  const isOnShowTriggered = ref(false)
  const isOnReadyTriggered = ref(false)
  const isOnPullDownRefreshTriggered = ref(false)
  const isOnPageScrollTriggered = ref(false)
  const isOnReachBottomTriggered = ref(false)
  const isOnBackPressTriggered = ref(false)
  const isOnHideTriggered = ref(false)
  const isOnResizeTriggered = ref(false)
61 62
  
  onLoad((_: OnLoadOptions) => {
63
    isOnloadTriggered.value = true
64
    // 自动化测试
65 66 67
    setLifeCycleNum(state.lifeCycleNum + 100)
  })
  onPageShow(() => {
68
    isOnShowTriggered.value = true
69
    // 自动化测试
70 71
    setLifeCycleNum(state.lifeCycleNum + 10)
  })
72
  onReady(() => {
73
    isOnReadyTriggered.value = true
74
    // 自动化测试
75 76 77
    setLifeCycleNum(state.lifeCycleNum + 10)
  })
  onPullDownRefresh(() => {
78
    isOnPullDownRefreshTriggered.value = true
79
    // 自动化测试
80 81
    setLifeCycleNum(state.lifeCycleNum + 10)
  })
82
  onPageScroll((_) => {
83
    isOnPageScrollTriggered.value = true
84 85 86
    // 自动化测试
    isScrolled.value = true
  })
87
  onReachBottom(() => {
88
    isOnReachBottomTriggered.value = true
89 90 91
    // 自动化测试
    setLifeCycleNum(state.lifeCycleNum + 10)
  })
92
  onBackPress((_: OnBackPressOptions): boolean | null => {
93
    isOnBackPressTriggered.value = true
94 95
    // 自动化测试
    setLifeCycleNum(state.lifeCycleNum - 10)
96 97
    return null
  })
98
  onPageHide(() => {
99
    isOnPageHideTriggered.value = true
100 101 102
    // 自动化测试
    setLifeCycleNum(state.lifeCycleNum - 10)
  })
103
  onUnload(() => {
104 105 106 107
    // 自动化测试
    setLifeCycleNum(state.lifeCycleNum - 100)
  })
  onResize((_) => {
108
    isOnResizeTriggered.value = true
109
    // 自动化测试
110
    setLifeCycleNum(state.lifeCycleNum + 10)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  })
  
  // 自动化测试
  const getLifeCycleNum = () : number => {
    return state.lifeCycleNum
  }
  // 自动化测试
  const pageSetLifeCycleNum = (num: number) => {
    setLifeCycleNum(num)
  }
 
  // 自动化测试
  const pullDownRefresh = () => {
    uni.startPullDownRefresh({
      success() {
        setTimeout(() => {
          uni.stopPullDownRefresh()
        }, 1500)
      },
    })
  }
132 133 134 135 136
  
  const scrollToBottom = () => {
    uni.pageScrollTo({
      scrollTop: 2000,
    })
137 138 139 140 141 142 143 144 145 146 147 148 149
  }
  
  // 自动化测试
  const getIsScrolled = (): boolean => {
    return isScrolled.value
  }
  
  defineExpose({
    getLifeCycleNum,
    pageSetLifeCycleNum,
    pullDownRefresh,
    scrollToBottom,
    getIsScrolled
150 151 152 153 154 155 156
  })
</script>

<style>
  .container {
    height: 1200px;
  }
157 158 159 160 161 162
  .item-container {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    flex-direction: row;
  }
163
</style>