navigator.uvue 11.2 KB
Newer Older
Y
init  
yurj26 已提交
1
<template>
2 3
  <view>
    <page-head title="navigate"></page-head>
DCloud-WZF's avatar
DCloud-WZF 已提交
4
    <view class="uni-padding-wrap uni-common-mt uni-common-mb">
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7
      <view class="direction-row">
        <text class="label">onLoad触发时间戳:</text>
        <text>{{ onLoadTime }}</text>
8
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
9 10 11
      <view class="direction-row">
        <text class="label">onShow触发时间戳:</text>
        <text>{{ onShowTime }}</text>
12
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
13 14 15
      <view class="direction-row">
        <text class="label">onReady触发时间戳:</text>
        <text>{{ onReadyTime }}</text>
16
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
17 18 19
      <view class="direction-row">
        <text class="label">onHide触发时间戳:</text>
        <text>{{ onHideTime }}</text>
20
      </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
21 22 23 24 25 26 27
      <view class="direction-row">
        <text class="label">onBackPress触发时间戳:</text>
        <text>见控制台</text>
      </view>
      <view class="direction-row">
        <text class="label">onUnload触发时间戳:</text>
        <text>见控制台</text>
28 29 30 31 32 33 34 35 36 37 38 39
      </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>
DCloud-WZF's avatar
DCloud-WZF 已提交
40 41 42
        <button @tap="navigateToErrorPage" class="uni-btn">
          打开不存在的页面
        </button>
43 44 45
      </view>
    </view>
  </view>
Y
init  
yurj26 已提交
46
</template>
DCloud-WZF's avatar
DCloud-WZF 已提交
47

Y
init  
yurj26 已提交
48
<script lang="ts">
DCloud-WZF's avatar
DCloud-WZF 已提交
49
import { state, setLifeCycleNum } from '@/store/index.uts'
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
export default {
  data() {
    return {
      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 已提交
73 74 75 76 77 78
    console.log('onHide', this.onHideTime)
  },
  onBackPress(options: OnBackPressOptions): boolean | null {
    console.log('onBackPress', Date.now())
    console.log('onBackPress from', options.from)
    return null
79 80
  },
  onUnload() {
DCloud-WZF's avatar
DCloud-WZF 已提交
81
    console.log('onUnload', Date.now())
82 83
  },
  methods: {
DCloud-WZF's avatar
DCloud-WZF 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    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)
        },
      })
    },
104 105
    navigateTo() {
      uni.navigateTo({
DCloud-WZF's avatar
DCloud-WZF 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        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 已提交
127 128
        success(result) {
          console.log('navigateTo success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
129 130
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
131 132 133
        },
        fail(error) {
          console.log('navigateTo fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
134 135 136 137
          uni.showToast({
            title: error.errMsg,
            icon: 'none',
          })
DCloud-WZF's avatar
DCloud-WZF 已提交
138 139
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
140 141 142
        },
        complete(result) {
          console.log('navigateTo complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
143 144 145 146 147 148 149 150 151 152 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
          // 自动化测试
          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 已提交
183
        },
184 185
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
186 187 188 189 190 191 192 193 194 195 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
    // 自动化测试
    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)
        },
      })
    },
231 232
    navigateBack() {
      uni.navigateBack({
DCloud-WZF's avatar
DCloud-WZF 已提交
233 234
        success(result) {
          console.log('navigateBack success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
235 236
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
237 238 239
        },
        fail(error) {
          console.log('navigateBack fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
240 241
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
242 243 244
        },
        complete(result) {
          console.log('navigateBack complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
245 246
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
247
        },
248 249
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
250 251 252 253 254 255 256 257 258 259 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
    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)
            },
          })
        },
      })
    },
300 301
    redirectTo() {
      uni.redirectTo({
DCloud-WZF's avatar
DCloud-WZF 已提交
302
        url: '/pages/API/navigator/new-page/new-page-1?data=Hello',
DCloud-WZF's avatar
DCloud-WZF 已提交
303 304
        success(result) {
          console.log('redirectTo success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
305 306
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
307 308 309
        },
        fail(error) {
          console.log('redirectTo fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
310 311
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
312 313 314
        },
        complete(result) {
          console.log('redirectTo complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
315 316
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
317
        },
318 319 320 321 322
      })
    },
    switchTab() {
      uni.switchTab({
        url: '/pages/tabBar/template',
DCloud-WZF's avatar
DCloud-WZF 已提交
323 324
        success(result) {
          console.log('switchTab success', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
325 326
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
327 328 329
        },
        fail(error) {
          console.log('switchTab fail', error.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
330 331
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum - 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
332 333 334
        },
        complete(result) {
          console.log('switchTab complete', result.errMsg)
DCloud-WZF's avatar
DCloud-WZF 已提交
335 336
          // 自动化测试
          setLifeCycleNum(state.lifeCycleNum + 1)
DCloud-WZF's avatar
DCloud-WZF 已提交
337
        },
338 339 340 341
      })
    },
    customAnimation() {
      uni.navigateTo({
DCloud-WZF's avatar
DCloud-WZF 已提交
342
        url: '/pages/API/navigator/new-page/new-page-1?data=自定义动画打开页面',
343 344 345 346
        animationType: 'slide-in-bottom',
        animationDuration: 200,
      })
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
347
    // 自动化测试
348 349 350
    getLifeCycleNum(): number {
      return state.lifeCycleNum
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
351 352 353 354
    // 自动化测试
    setLifeCycleNum(num: number) {
      setLifeCycleNum(num)
    },
355 356 357
  },
}
</script>
DCloud-WZF's avatar
DCloud-WZF 已提交
358 359 360 361 362 363 364 365 366

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