navigator.uvue 11.4 KB
Newer Older
Y
init  
yurj26 已提交
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
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
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #endif -->
    <view>
      <page-head title="navigate"></page-head>
      <view class="uni-padding-wrap uni-common-mt uni-common-mb">
        <view class="direction-row">
          <text class="label">onLoad触发时间戳:</text>
          <text>{{ onLoadTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onShow触发时间戳:</text>
          <text>{{ onShowTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onReady触发时间戳:</text>
          <text>{{ onReadyTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onHide触发时间戳:</text>
          <text>{{ onHideTime }}</text>
        </view>
        <view class="direction-row">
          <text class="label">onBackPress触发时间戳:</text>
          <text>见控制台</text>
        </view>
        <view class="direction-row">
          <text class="label">onUnload触发时间戳:</text>
          <text>见控制台</text>
        </view>
        <view class="uni-btn-v">
          <button @tap="navigateTo" class="uni-btn">
            跳转新页面,并传递数据
          </button>
          <button @tap="navigateBack" class="uni-btn">返回上一页</button>
          <button @tap="redirectTo" class="uni-btn">在当前页面打开</button>
          <button @tap="switchTab" class="uni-btn">切换到模板选项卡</button>
          <button @tap="reLaunch" class="uni-btn">
            关闭所有页面,打开首页
          </button>
          <button @tap="customAnimation" class="uni-btn">
            使用自定义动画打开页面
          </button>
          <button @tap="navigateToErrorPage" class="uni-btn">
            打开不存在的页面
          </button>
        </view>
49 50
      </view>
    </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
51
    <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
52
  </scroll-view>
DCloud-WZF's avatar
DCloud-WZF 已提交
53
  <!-- #endif -->
Y
init  
yurj26 已提交
54
</template>
DCloud-WZF's avatar
DCloud-WZF 已提交
55

DCloud-WZF's avatar
DCloud-WZF 已提交
56 57 58
<script lang="uts">
import { State, state, setLifeCycleNum } from '@/store/index.uts'

59
export default {
DCloud-WZF's avatar
DCloud-WZF 已提交
60
  data() {
61
    return {
DCloud-WZF's avatar
DCloud-WZF 已提交
62
      _state: state as State,
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      onLoadTime: 0,
      onShowTime: 0,
      onReadyTime: 0,
      onHideTime: 0,
    }
  },
  onLoad() {
    this.onLoadTime = Date.now()
    console.log('onLoad', this.onLoadTime)
  },
  onShow() {
    this.onShowTime = Date.now()
    console.log('onShow', this.onShowTime)
  },
  onReady() {
    this.onReadyTime = Date.now()
    console.log('onReady', this.onReadyTime)
  },
  onHide() {
    this.onHideTime = Date.now()
DCloud-WZF's avatar
DCloud-WZF 已提交
83 84 85 86 87 88
    console.log('onHide', this.onHideTime)
  },
  onBackPress(options: OnBackPressOptions): boolean | null {
    console.log('onBackPress', Date.now())
    console.log('onBackPress from', options.from)
    return null
89 90
  },
  onUnload() {
DCloud-WZF's avatar
DCloud-WZF 已提交
91
    console.log('onUnload', Date.now())
92 93
  },
  methods: {
DCloud-WZF's avatar
DCloud-WZF 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    reLaunch() {
      uni.reLaunch({
        url: '/pages/tabBar/component',
        success(result) {
          console.log('reLaunch success', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
        fail(error) {
          console.log('reLaunch fail', error.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
        },
        complete(result) {
          console.log('reLaunch complete', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
      })
    },
114 115
    navigateTo() {
      uni.navigateTo({
DCloud-WZF's avatar
DCloud-WZF 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        url: '/pages/API/navigator/new-page/new-page-1?data=Hello',
        success(result) {
          console.log('navigateTo success', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
        fail(error) {
          console.log('navigateTo fail', error.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
        },
        complete(result) {
          console.log('navigateTo complete', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
      })
    },
    navigateToErrorPage() {
      uni.navigateTo({
        url: '/pages/error-page/error-page',
DCloud-WZF's avatar
DCloud-WZF 已提交
137 138
        success(result) {
          console.log('navigateTo success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
139 140
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
141 142 143
        },
        fail(error) {
          console.log('navigateTo fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
144 145 146 147
          uni.showToast({
            title: error.errMsg,
            icon: 'none',
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
148 149
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
150 151 152
        },
        complete(result) {
          console.log('navigateTo complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
      })
    },
    navigateToDebounce() {
      uni.navigateTo({
        url: '/pages/API/navigator/new-page/new-page-1?data=debounce',
        success(result) {
          console.log('navigateTo success', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
        fail(error) {
          console.log('navigateTo fail', error.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
        },
        complete(result) {
          console.log('navigateTo complete', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
      })
      uni.navigateTo({
        url: '/pages/API/navigator/new-page/new-page-1?data=debounce',
        success(result) {
          console.log('navigateTo success', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
        },
        fail(error) {
          console.log('navigateTo fail', error.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
        complete(result) {
          console.log('navigateTo complete', result.errMsg)
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
193
        },
194 195
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    // 自动化测试
    navigateToRelativePath1() {
      uni.navigateTo({
        url: 'new-page/new-page-1?data=new-page/new-page-1',
        success() {
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
        fail() {
          setLifeCycleNum(state.lifeCycleNum - 1)
        },
        complete() {
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
      })
    },
    // 自动化测试
    navigateToRelativePath2() {
      uni.navigateTo({
        url: './new-page/new-page-1?data=./new-page/new-page-1',
        success() {
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
        fail() {
          setLifeCycleNum(state.lifeCycleNum - 1)
        },
        complete() {
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
      })
    },
    // 自动化测试
    navigateToRelativePath3() {
      uni.navigateTo({
        url: '../navigator/new-page/new-page-1?data=../navigator/new-page/new-page-1',
        success() {
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
        fail() {
          setLifeCycleNum(state.lifeCycleNum - 1)
        },
        complete() {
          setLifeCycleNum(state.lifeCycleNum + 1)
        },
      })
    },
241 242
    navigateBack() {
      uni.navigateBack({
DCloud-WZF's avatar
DCloud-WZF 已提交
243 244
        success(result) {
          console.log('navigateBack success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
245 246
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
247 248 249
        },
        fail(error) {
          console.log('navigateBack fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
250 251
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
252 253 254
        },
        complete(result) {
          console.log('navigateBack complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
255 256
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
257
        },
258 259
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    navigateBackWithDelta1() {
      uni.navigateTo({
        url: '/pages/API/navigator/new-page/new-page-1',
        success() {
          uni.navigateBack({
            delta: 1,
            success(result) {
              console.log('navigateBack success', result.errMsg)
              // 自动化测试
              setLifeCycleNum(state.lifeCycleNum + 1)
            },
            fail(error) {
              console.log('navigateBack fail', error.errMsg)
              // 自动化测试
              setLifeCycleNum(state.lifeCycleNum - 1)
            },
            complete(result) {
              console.log('navigateBack complete', result.errMsg)
              // 自动化测试
              setLifeCycleNum(state.lifeCycleNum + 1)
            },
          })
        },
      })
    },
    navigateBackWithDelta100() {
      uni.navigateTo({
        url: '/pages/API/navigator/new-page/new-page-1',
        success() {
          uni.navigateBack({
            delta: 100,
            success(result) {
              console.log('navigateBack success', result.errMsg)
              // 自动化测试
              setLifeCycleNum(state.lifeCycleNum + 1)
            },
            fail(error) {
              console.log('navigateBack fail', error.errMsg)
              // 自动化测试
              setLifeCycleNum(state.lifeCycleNum - 1)
            },
            complete(result) {
              console.log('navigateBack complete', result.errMsg)
              // 自动化测试
              setLifeCycleNum(state.lifeCycleNum + 1)
            },
          })
        },
      })
    },
310 311
    redirectTo() {
      uni.redirectTo({
DCloud-WZF's avatar
DCloud-WZF 已提交
312
        url: '/pages/API/navigator/new-page/new-page-1?data=Hello',
DCloud-WZF's avatar
DCloud-WZF 已提交
313 314
        success(result) {
          console.log('redirectTo success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
315 316
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
317 318 319
        },
        fail(error) {
          console.log('redirectTo fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
320 321
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
322 323 324
        },
        complete(result) {
          console.log('redirectTo complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
325 326
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
327
        },
328 329 330 331 332
      })
    },
    switchTab() {
      uni.switchTab({
        url: '/pages/tabBar/template',
DCloud-WZF's avatar
DCloud-WZF 已提交
333 334
        success(result) {
          console.log('switchTab success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
335 336
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
337 338 339
        },
        fail(error) {
          console.log('switchTab fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
340 341
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
342 343 344
        },
        complete(result) {
          console.log('switchTab complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
345 346
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
347
        },
348 349 350 351
      })
    },
    customAnimation() {
      uni.navigateTo({
DCloud-WZF's avatar
DCloud-WZF 已提交
352
        url: '/pages/API/navigator/new-page/new-page-2?data=自定义动画打开页面',
353 354 355 356
        animationType: 'slide-in-bottom',
        animationDuration: 200,
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
357
    // 自动化测试
358 359 360
    getLifeCycleNum(): number {
      return state.lifeCycleNum
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
361 362 363 364
    // 自动化测试
    setLifeCycleNum(num: number) {
      setLifeCycleNum(num)
    },
365 366 367
  },
}
</script>
DCloud-WZF's avatar
DCloud-WZF 已提交
368 369 370 371 372 373 374 375 376

<style>
.direction-row {
  flex-direction: row;
}
.label {
  width: 190px;
}
</style>