show-toast.uvue 6.2 KB
Newer Older
1
<template>
Anne_LXM's avatar
Anne_LXM 已提交
2 3 4
  <!-- #ifdef APP -->
  <scroll-view direction="vertical" style="flex:1">
  <!-- #endif -->
5 6
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
Anne_LXM's avatar
Anne_LXM 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
      <view class="uni-padding-wrap">
        <text class="uni-title-text uni-common-mb">设置icon</text>
      </view>
      <view class="uni-list uni-common-pl">
        <radio-group @change="radioChangeIcon">
          <radio class="uni-list-cell uni-list-cell-pd radio-icon" v-for="(icon, index) in icon_enum" :key="icon.value"
            :class="index < icon_enum.length - 1 ? 'uni-list-cell-line' : ''" :value="icon.value"
            :checked="index === icon_current">{{icon.name}}</radio>
        </radio-group>
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <view class="uni-list-cell-db">是否显示自定义图标</view>
        <switch :checked="imageSelect" @change="change_image_boolean" />
      </view>
      <view class="uni-list-cell uni-list-cell-padding">
        <view class="uni-list-cell-db">是否显示透明蒙层-屏蔽点击事件</view>
        <switch :checked="maskSelect" @change="change_mask_boolean" />
      </view>
      <view class="uni-title uni-list-cell-padding">提示的延迟时间,默认:1500(单位毫秒)</view>
      <view class="uni-list-cell-padding">
        <slider @change="sliderChange" foreColor="#007AFF" :value="intervalSelect" :min="1500" :max="5000" :show-value="true" />
      </view>
29
      <view class="uni-btn-v">
Anne_LXM's avatar
Anne_LXM 已提交
30
        <button class="uni-btn-v" type="default" @tap="toast1Tap" id="btn-toast-default">点击弹出toast</button>
杜庆泉's avatar
杜庆泉 已提交
31
        <button class="uni-btn-v" type="default" @tap="hideToast" id="btn-toast-hide">点击隐藏toast</button>
32
      </view>
Anne_LXM's avatar
Anne_LXM 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
      <!-- #ifdef APP -->
      <view class="uni-padding-wrap uni-common-mt">
        <text class="uni-title-text uni-common-mb"> 设置position,仅App生效 </text>
      </view>
      <view class="uni-list uni-common-pl">
         <radio-group @change="radioChangePosition">
           <radio class="uni-list-cell uni-list-cell-pd radio-position" v-for="(position, index) in position_enum" :key="position.value"
             :class="index < position_enum.length - 1 ? 'uni-list-cell-line' : ''" :value="position.value"
             :checked="index === position_current">{{position.name}}</radio>
         </radio-group>
      </view>
      <button class="uni-btn uni-btn-v uni-common-mb" type="default" @tap="toast2Tap">点击弹出设置position的toast</button>
      <!-- #endif -->
46 47
      <text>{{exeRet}}</text>
    </view>
Anne_LXM's avatar
Anne_LXM 已提交
48 49 50
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
51
</template>
H
hdx 已提交
52

Anne_LXM's avatar
Anne_LXM 已提交
53 54 55 56 57 58 59 60 61
<script lang="uts">
  type IconItemType = {
    value : "success" | "error" | "fail" | "exception" | "loading" | "none";
    name : string
  }
  type PositionItemType = {
    value : "top" | "center" | "bottom";
    name : string
  }
62 63 64 65
  export default {
    data() {
      return {
        title: 'toast',
Anne_LXM's avatar
Anne_LXM 已提交
66 67 68 69 70 71
        exeRet: '',
        imageSelect:false,
        maskSelect: false,
        intervalSelect: 1500,
        position_current:0,
        position_enum: [
Anne_LXM's avatar
Anne_LXM 已提交
72 73
          { "value": "top", "name": "top: 居上显示(Android 暂不支持)" },
          { "value": "center", "name": "center: 居中显示(Android 暂不支持)" },
Anne_LXM's avatar
Anne_LXM 已提交
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
          { "value": "bottom", "name": "bottom: 居底显示" },
        ] as PositionItemType[],
        icon_current:0,
        icon_enum: [
          {
            value: 'success',
            name: '显示成功图标',
          },
          {
            value: 'error',
            name: '显示错误图标',
          },
          // {
          //   value: 'fail',
          //   name: '显示错误图标',
          // },
          // {
          //   value: 'exception',
          //   name: '显示异常图标,此时title文本无长度显示',
          // },
          {
            value: 'loading',
            name: '显示加载图标',
          },
          {
            value: 'none',
            name: '不显示图标',
          },
        ] as IconItemType[],
103 104
      }
    },
H
hdx 已提交
105
    onLoad() {
杜庆泉's avatar
杜庆泉 已提交
106
      uni.showToast({
H
hdx 已提交
107
        title: 'onLoad 调用示例,2秒后消失'
杜庆泉's avatar
杜庆泉 已提交
108
      })
H
hdx 已提交
109
      setTimeout(function () {
杜庆泉's avatar
杜庆泉 已提交
110
        uni.hideToast()
111
      }, 2000);
杜庆泉's avatar
杜庆泉 已提交
112
    },
113
    methods: {
114 115 116 117
      //自动化测试例专用
      jest_getWindowInfo() : GetWindowInfoResult {
        return uni.getWindowInfo();
      },
Anne_LXM's avatar
Anne_LXM 已提交
118 119 120 121 122 123 124
      radioChangeIcon(e : UniRadioGroupChangeEvent) {
        for (let i = 0; i < this.icon_enum.length; i++) {
          if (this.icon_enum[i].value === e.detail.value) {
            this.icon_current = i;
            break;
          }
        }
125
      },
Anne_LXM's avatar
Anne_LXM 已提交
126 127
      change_image_boolean: function (e : UniSwitchChangeEvent) {
        this.imageSelect = e.detail.value
128
      },
Anne_LXM's avatar
Anne_LXM 已提交
129 130
      change_mask_boolean: function (e : UniSwitchChangeEvent) {
        this.maskSelect = e.detail.value
131
      },
Anne_LXM's avatar
Anne_LXM 已提交
132 133 134 135 136 137 138 139 140 141
      sliderChange(e : UniSliderChangeEvent) {
        this.intervalSelect = e.detail.value
      },
      radioChangePosition(e : UniRadioGroupChangeEvent) {
        for (let i = 0; i < this.position_enum.length; i++) {
          if (this.position_enum[i].value === e.detail.value) {
            this.position_current = i;
            break;
          }
        }
142
      },
Anne_LXM's avatar
Anne_LXM 已提交
143
      toast1Tap: function () {
144
        uni.showToast({
Anne_LXM's avatar
Anne_LXM 已提交
145 146 147 148 149
          title: "默认",
          icon: this.icon_enum[this.icon_current].value,
          duration: this.intervalSelect,
          image: this.imageSelect? "/static/uni.png" : null ,
          mask: this.maskSelect,
150
          success: (res) => {
Anne_LXM's avatar
Anne_LXM 已提交
151
            // console.log('success:',res)
杜庆泉's avatar
杜庆泉 已提交
152
            this.exeRet = "success:" + JSON.stringify(res)
153 154 155 156 157 158
          },
          fail: (res) => {
            this.exeRet = "fail:" + JSON.stringify(res)
          },
        })
      },
159
      // #ifdef APP
Anne_LXM's avatar
Anne_LXM 已提交
160 161
      toast2Tap: function () {
        let positionValue = this.position_enum[this.position_current].value
162
        uni.showToast({
Anne_LXM's avatar
Anne_LXM 已提交
163 164
          title: "显示一段轻提示,position:" + positionValue,
          position: positionValue,
165
          success: (res) => {
杜庆泉's avatar
杜庆泉 已提交
166
            this.exeRet = "success:" + JSON.stringify(res)
167 168 169 170 171 172 173 174 175 176 177 178
          },
          fail: (res) => {
            this.exeRet = "fail:" + JSON.stringify(res)
          },
        })
      },
      // #endif
      hideToast: function () {
        uni.hideToast()
      }
    }
  }
Y
yurj26 已提交
179
</script>
Anne_LXM's avatar
Anne_LXM 已提交
180