onLoad.uvue 2.8 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 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 49 50 51 52 53 54 55 56 57
<template>
  <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>
</template>

<script lang="uts">
export default {
  data() {
    return {
      isTrue: false,
      isFalse: true,
      msg: 'default msg'
    }
  },
  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;
    }
  },
  // #ifdef WEB
  onUnload() {
雪洛's avatar
雪洛 已提交
58 59
    // web 端页面销毁前,关闭 modal 和 actionsheet
    // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
60 61 62
    const modalBtn = document.querySelector('.uni-modal__btn')
    if (modalBtn) {
      modalBtn.click()
雪洛's avatar
雪洛 已提交
63 64
    }
    // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
65 66 67 68 69 70 71 72 73 74 75 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
    const actionSheetBtn = document.querySelector('.uni-actionsheet__action .uni-actionsheet__cell')
    if (actionSheetBtn) {
      actionSheetBtn.click()
    }
  },
  // #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']
      })
    }
  }
}
</script>