show-toast.uvue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view direction="vertical" style="flex:1">
  4. <!-- #endif -->
  5. <page-head :title="title"></page-head>
  6. <view class="uni-padding-wrap">
  7. <view class="uni-padding-wrap">
  8. <text class="uni-title-text uni-common-mb">设置icon</text>
  9. </view>
  10. <view class="uni-list uni-common-pl">
  11. <radio-group @change="radioChangeIcon">
  12. <radio class="uni-list-cell uni-list-cell-pd radio-icon" v-for="(icon, index) in icon_enum" :key="icon.value"
  13. :class="index < icon_enum.length - 1 ? 'uni-list-cell-line' : ''" :value="icon.value"
  14. :checked="index === icon_current">{{icon.name}}</radio>
  15. </radio-group>
  16. </view>
  17. <view class="uni-list-cell uni-list-cell-padding">
  18. <view class="uni-list-cell-db">是否显示自定义图标</view>
  19. <switch :checked="imageSelect" @change="change_image_boolean" />
  20. </view>
  21. <view class="uni-list-cell uni-list-cell-padding">
  22. <view class="uni-list-cell-db">是否显示透明蒙层-屏蔽点击事件</view>
  23. <switch :checked="maskSelect" @change="change_mask_boolean" />
  24. </view>
  25. <view class="uni-title uni-list-cell-padding">提示的延迟时间,默认:1500(单位毫秒)</view>
  26. <view class="uni-list-cell-padding">
  27. <slider @change="sliderChange" foreColor="#007AFF" :value="intervalSelect" :min="1500" :max="5000"
  28. :show-value="true" />
  29. </view>
  30. <view class="uni-btn-v">
  31. <button class="uni-btn-v" type="default" @tap="toast1Tap" id="btn-toast-default">点击弹出toast</button>
  32. <button class="uni-btn-v" type="default" @tap="hideToast" id="btn-toast-hide">点击隐藏toast</button>
  33. </view>
  34. <!-- #ifdef APP -->
  35. <view class="uni-padding-wrap uni-common-mt">
  36. <text class="uni-title-text uni-common-mb"> 设置position,仅App生效 </text>
  37. </view>
  38. <view class="uni-list uni-common-pl">
  39. <radio-group @change="radioChangePosition">
  40. <radio class="uni-list-cell uni-list-cell-pd radio-position" v-for="(position, index) in position_enum"
  41. :key="position.value" :class="index < position_enum.length - 1 ? 'uni-list-cell-line' : ''"
  42. :value="position.value" :checked="index === position_current">{{position.name}}</radio>
  43. </radio-group>
  44. </view>
  45. <button class="uni-btn uni-btn-v uni-common-mb" type="default" @tap="toast2Tap">点击弹出设置position的toast</button>
  46. <!-- #endif -->
  47. <text>{{exeRet}}</text>
  48. </view>
  49. <!-- #ifdef APP -->
  50. </scroll-view>
  51. <!-- #endif -->
  52. </template>
  53. <script lang="uts">
  54. type IconItemType = {
  55. value : "success" | "error" | "fail" | "exception" | "loading" | "none";
  56. name : string
  57. }
  58. type PositionItemType = {
  59. value : "top" | "center" | "bottom";
  60. name : string
  61. }
  62. export default {
  63. data() {
  64. return {
  65. title: 'toast',
  66. exeRet: '',
  67. imageSelect: false,
  68. maskSelect: false,
  69. intervalSelect: 1500,
  70. position_current: 0,
  71. position_enum: [
  72. { "value": "top", "name": "top: 居上显示(Android 暂不支持)" },
  73. { "value": "center", "name": "center: 居中显示(Android 暂不支持)" },
  74. { "value": "bottom", "name": "bottom: 居底显示" },
  75. ] as PositionItemType[],
  76. icon_current: 0,
  77. icon_enum: [
  78. {
  79. value: 'success',
  80. name: '显示成功图标',
  81. },
  82. {
  83. value: 'error',
  84. name: '显示错误图标',
  85. },
  86. // {
  87. // value: 'fail',
  88. // name: '显示错误图标',
  89. // },
  90. // {
  91. // value: 'exception',
  92. // name: '显示异常图标,此时title文本无长度显示',
  93. // },
  94. {
  95. value: 'loading',
  96. name: '显示加载图标',
  97. },
  98. {
  99. value: 'none',
  100. name: '不显示图标',
  101. },
  102. ] as IconItemType[],
  103. }
  104. },
  105. onLoad() {
  106. uni.showToast({
  107. title: 'onLoad 调用示例,2秒后消失'
  108. })
  109. setTimeout(function () {
  110. uni.hideToast()
  111. }, 2000);
  112. },
  113. methods: {
  114. //自动化测试例专用
  115. jest_getWindowInfo() : GetWindowInfoResult {
  116. return uni.getWindowInfo();
  117. },
  118. radioChangeIcon(e : UniRadioGroupChangeEvent) {
  119. for (let i = 0; i < this.icon_enum.length; i++) {
  120. if (this.icon_enum[i].value === e.detail.value) {
  121. this.icon_current = i;
  122. break;
  123. }
  124. }
  125. },
  126. change_image_boolean: function (e : UniSwitchChangeEvent) {
  127. this.imageSelect = e.detail.value
  128. },
  129. change_mask_boolean: function (e : UniSwitchChangeEvent) {
  130. this.maskSelect = e.detail.value
  131. },
  132. sliderChange(e : UniSliderChangeEvent) {
  133. this.intervalSelect = e.detail.value
  134. },
  135. radioChangePosition(e : UniRadioGroupChangeEvent) {
  136. for (let i = 0; i < this.position_enum.length; i++) {
  137. if (this.position_enum[i].value === e.detail.value) {
  138. this.position_current = i;
  139. break;
  140. }
  141. }
  142. },
  143. toast1Tap: function () {
  144. uni.showToast({
  145. title: "默认",
  146. icon: this.icon_enum[this.icon_current].value,
  147. duration: this.intervalSelect,
  148. image: this.imageSelect ? "/static/uni.png" : null,
  149. mask: this.maskSelect,
  150. success: (res) => {
  151. // console.log('success:',res)
  152. this.exeRet = "success:" + JSON.stringify(res)
  153. },
  154. fail: (res) => {
  155. this.exeRet = "fail:" + JSON.stringify(res)
  156. },
  157. })
  158. },
  159. toast3Tap: function () {
  160. uni.showToast({
  161. title: "默认",
  162. icon: 'none',
  163. duration: this.intervalSelect,
  164. image: this.imageSelect ? "/static/uni.png" : null,
  165. mask: this.maskSelect,
  166. success: (res) => {
  167. // console.log('success:',res)
  168. this.exeRet = "success:" + JSON.stringify(res)
  169. },
  170. fail: (res) => {
  171. this.exeRet = "fail:" + JSON.stringify(res)
  172. },
  173. })
  174. },
  175. // #ifdef APP
  176. toast2Tap: function () {
  177. let positionValue = this.position_enum[this.position_current].value
  178. uni.showToast({
  179. title: "显示一段轻提示,position:" + positionValue,
  180. position: positionValue,
  181. success: (res) => {
  182. this.exeRet = "success:" + JSON.stringify(res)
  183. },
  184. fail: (res) => {
  185. this.exeRet = "fail:" + JSON.stringify(res)
  186. },
  187. })
  188. },
  189. // #endif
  190. hideToast: function () {
  191. uni.hideToast()
  192. }
  193. }
  194. }
  195. </script>