onLoad.uvue 3.2 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
<template>
2 3 4
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7 8 9 10 11 12
    <view class="uni-padding-wrap">
      <page-head title="onLoad 生命周期调用 uni api 测试" />
      <text v-if="isTrue">v-if with true</text>
      <text v-if="isFalse">v-if with false</text>
      <text v-show="isTrue">v-show with true</text>
      <text v-show="isFalse">v-show with false</text>
      <text>msg: {{ msg }}</text>
    </view>
13 14 15
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
16 17 18
</template>

<script lang="uts">
DCloud-WZF's avatar
DCloud-WZF 已提交
19 20 21 22 23 24 25
  export default {
    data() {
      return {
        isTrue: false,
        isFalse: true,
        msg: 'default msg'
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
26
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    onLoad(options : OnLoadOptions) {
      const type = options['type']
      switch (type) {
        case 'adjustData':
          this.adjustData()
          break;
        case 'navigateTo':
          this.navigateTo()
          break;
        case 'navigateBack':
          this.navigateBack()
          break;
        case 'redirectTo':
          this.redirectTo()
          break;
        case 'reLaunch':
          this.reLaunch()
          break;
        case 'switchTab':
          this.switchTab()
          break;
        case 'showToast':
          this.showToast()
          break;
        case 'showLoading':
          this.showLoading()
          break;
        case 'showModal':
          this.showModal()
          break;
        case 'showActionSheet':
          this.showActionSheet()
          break;
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
61
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
    // #ifdef WEB
    onUnload() {
      // web 端页面销毁前,关闭 modal 和 actionsheet
      // @ts-ignore
      const modalBtn = document.querySelector('.uni-modal__btn')
      if (modalBtn) {
        modalBtn.click()
      }
      // @ts-ignore
      const actionSheetBtn = document.querySelector('.uni-actionsheet__action .uni-actionsheet__cell')
      if (actionSheetBtn) {
        actionSheetBtn.click()
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
75
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    // #endif
    methods: {
      adjustData() {
        this.isTrue = true
        this.isFalse = false
        this.msg = 'new msg'
      },
      navigateTo() {
        uni.navigateTo({
          url: '/pages/API/navigator/new-page/new-page-3'
        })
      },
      navigateBack() {
        uni.navigateBack()
      },
      redirectTo() {
        uni.redirectTo({
          url: '/pages/API/navigator/new-page/new-page-3'
        })
      },
      reLaunch() {
        uni.reLaunch({
          url: '/pages/API/navigator/new-page/new-page-3'
        })
      },
      switchTab() {
        uni.switchTab({
          url: '/pages/tabBar/component'
        })
      },
      showToast() {
        uni.showToast({
          title: 'test title',
          icon: 'success',
          duration: 2000
        })
      },
      showLoading() {
        uni.showLoading({
          title: 'test title',
        })
      },
      showModal() {
        uni.showModal({
          title: 'test title',
          content: 'test content'
        })
      },
      showActionSheet() {
        uni.showActionSheet({
          title: 'test title',
          itemList: ['1', '2', '3']
        })
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
130 131
    }
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
132
</script>