navigator.uvue 10.9 KB
Newer Older
Y
init  
yurj26 已提交
1
<template>
2 3 4
  <view>
    <page-head title="navigate"></page-head>
    <view class="uni-padding-wrap uni-common-mt">
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 40 41 42
      </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>
      </view>
    </view>
  </view>
Y
init  
yurj26 已提交
43
</template>
DCloud-WZF's avatar
DCloud-WZF 已提交
44

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

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