popup.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <view class="container">
  3. <uni-card is-full :is-shadow="false">
  4. <text class="uni-h6">弹出层组件用于弹出一个覆盖到页面上的内容,使用场景如:底部弹出分享弹窗、页面插屏广告等。</text>
  5. </uni-card>
  6. <uni-section title="基本示例" type="line" sub-title="自定义顶部圆角">
  7. <view class="example-body box">
  8. <button class="button" type="primary" @click="toggle('top')"><text class="button-text">顶部</text></button>
  9. <button class="button" type="primary" @click="toggle('bottom')"><text class="button-text">底部</text></button>
  10. <button class="button" type="primary" @click="toggle('center')"><text class="button-text">居中</text></button>
  11. <button class="button" type="primary" @click="toggle('left')"><text class="button-text">左侧</text></button>
  12. <button class="button" type="primary" @click="toggle('right')"><text class="button-text">右侧</text></button>
  13. </view>
  14. </uni-section>
  15. <uni-section title="提示消息" type="line">
  16. <view class="example-body box">
  17. <button class="button popup-success" @click="messageToggle('success')"><text
  18. class="button-text success-text">成功</text></button>
  19. <button class="button popup-error" @click="messageToggle('error')"><text
  20. class="button-text error-text">失败</text></button>
  21. <button class="button popup-warn" @click="messageToggle('warn')"><text
  22. class="button-text warn-text">警告</text></button>
  23. <button class="button popup-info" @click="messageToggle('info')"><text
  24. class="button-text info-text">信息</text></button>
  25. </view>
  26. </uni-section>
  27. <uni-section title="对话框示例" type="line" class="hideOnPc">
  28. <view class="example-body box">
  29. <button class="button popup-success" @click="dialogToggle('success')"><text
  30. class="button-text success-text">成功</text></button>
  31. <button class="button popup-error" @click="dialogToggle('error')"><text
  32. class="button-text error-text">失败</text></button>
  33. <button class="button popup-warn" @click="dialogToggle('warn')"><text
  34. class="button-text warn-text">警告</text></button>
  35. <button class="button popup-info" @click="dialogToggle('info')"><text
  36. class="button-text info-text">信息</text></button>
  37. </view>
  38. </uni-section>
  39. <uni-section title="输入框示例" type="line" padding>
  40. <view class="dialog-box">
  41. <text class="dialog-text">输入内容:{{ value }}</text>
  42. </view>
  43. <button class="button" type="primary" @click="inputDialogToggle"><text class="button-text">输入对话框</text></button>
  44. </uni-section>
  45. <uni-section title="底部分享示例" type="line" padding>
  46. <button class="button" type="primary" @click="shareToggle"><text class="button-text">分享模版示例</text></button>
  47. </uni-section>
  48. <view>
  49. <!-- 普通弹窗 -->
  50. <uni-popup ref="popup" background-color="#fff" @change="change" border-radius="10px 10px 0 0">
  51. <view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }"><text
  52. class="text">popup 内容</text></view>
  53. </uni-popup>
  54. </view>
  55. <view>
  56. <!-- 提示信息弹窗 -->
  57. <uni-popup ref="message" type="message">
  58. <uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
  59. </uni-popup>
  60. </view>
  61. <view>
  62. <!-- 提示窗示例 -->
  63. <uni-popup ref="alertDialog" type="dialog">
  64. <uni-popup-dialog :showClose="showClose" :type="msgType" cancelText="关闭" confirmText="同意" title="通知"
  65. content="欢迎使用 uni-popup!" @confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
  66. </uni-popup>
  67. </view>
  68. <view>
  69. <!-- 输入框示例 -->
  70. <uni-popup ref="inputDialog" type="dialog">
  71. <uni-popup-dialog ref="inputClose" :maxlength="10" mode="input" title="输入内容" v-model="value"
  72. placeholder="请输入内容,限制10个字" @confirm="dialogInputConfirm"></uni-popup-dialog>
  73. </uni-popup>
  74. </view>
  75. <view>
  76. <!-- 分享示例 -->
  77. <uni-popup ref="share" type="share" safeArea backgroundColor="#fff">
  78. <uni-popup-share></uni-popup-share>
  79. </uni-popup>
  80. </view>
  81. </view>
  82. </template>
  83. <script setup>
  84. import { ref, reactive } from 'vue';
  85. // 定义响应式数据
  86. const state = reactive({
  87. type: 'center',
  88. msgType: 'success',
  89. messageText: '这是一条成功提示',
  90. showClose: true,
  91. });
  92. // 定义普通响应式变量
  93. const value = ref('');
  94. // 获取对uni-popup组件的引用
  95. const popup = ref(null);
  96. const message = ref(null);
  97. const alertDialog = ref(null);
  98. const inputDialog = ref(null);
  99. const share = ref(null);
  100. // 定义方法
  101. const change = (e) => {
  102. console.log('当前模式:' + e.type + ',状态:' + e.show);
  103. };
  104. const toggle = (type) => {
  105. state.type = type;
  106. // open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
  107. popup.value.open(type);
  108. };
  109. const messageToggle = (type) => {
  110. state.msgType = type;
  111. state.messageText = `这是一条${type}消息提示`;
  112. message.value.open();
  113. };
  114. const dialogToggle = (type) => {
  115. state.msgType = type;
  116. alertDialog.value.open();
  117. };
  118. const dialogConfirm = () => {
  119. console.log('点击确认');
  120. state.messageText = `点击确认了 ${state.msgType} 窗口`;
  121. message.value.open();
  122. };
  123. const inputDialogToggle = () => {
  124. inputDialog.value.open();
  125. };
  126. const dialogClose = () => {
  127. console.log('点击关闭');
  128. };
  129. const dialogInputConfirm = (val) => {
  130. uni.showLoading({ title: '3秒后会关闭' });
  131. console.log(val); // 注意这里改为直接打印传入的值
  132. setTimeout(() => {
  133. uni.hideLoading();
  134. value.value = val;
  135. // 关闭窗口后,恢复默认内容
  136. inputDialog.value.close();
  137. }, 3000);
  138. };
  139. const shareToggle = () => {
  140. share.value.open();
  141. };
  142. </script>
  143. <style lang="scss">
  144. @mixin flex {
  145. /* #ifndef APP-NVUE */
  146. display: flex;
  147. /* #endif */
  148. flex-direction: row;
  149. }
  150. @mixin height {
  151. /* #ifndef APP-NVUE */
  152. height: 100%;
  153. /* #endif */
  154. /* #ifdef APP-NVUE */
  155. flex: 1;
  156. /* #endif */
  157. }
  158. .box {
  159. @include flex;
  160. }
  161. .button {
  162. @include flex;
  163. align-items: center;
  164. justify-content: center;
  165. flex: 1;
  166. height: 35px;
  167. margin: 0 5px;
  168. border-radius: 5px;
  169. }
  170. .example-body {
  171. background-color: #fff;
  172. padding: 10px 0;
  173. }
  174. .button-text {
  175. color: #fff;
  176. font-size: 12px;
  177. }
  178. .popup-content {
  179. @include flex;
  180. align-items: center;
  181. justify-content: center;
  182. padding: 15px;
  183. height: 50px;
  184. }
  185. .popup-height {
  186. @include height;
  187. width: 200px;
  188. }
  189. .text {
  190. font-size: 12px;
  191. color: #333;
  192. }
  193. .popup-success {
  194. color: #fff;
  195. background-color: #e1f3d8;
  196. }
  197. .popup-warn {
  198. color: #fff;
  199. background-color: #faecd8;
  200. }
  201. .popup-error {
  202. color: #fff;
  203. background-color: #fde2e2;
  204. }
  205. .popup-info {
  206. color: #fff;
  207. background-color: #f2f6fc;
  208. }
  209. .success-text {
  210. color: #09bb07;
  211. }
  212. .warn-text {
  213. color: #e6a23c;
  214. }
  215. .error-text {
  216. color: #f56c6c;
  217. }
  218. .info-text {
  219. color: #909399;
  220. }
  221. .dialog,
  222. .share {
  223. /* #ifndef APP-NVUE */
  224. display: flex;
  225. /* #endif */
  226. flex-direction: column;
  227. }
  228. .dialog-box {
  229. padding: 10px;
  230. }
  231. .dialog .button,
  232. .share .button {
  233. /* #ifndef APP-NVUE */
  234. width: 100%;
  235. /* #endif */
  236. margin: 0;
  237. margin-top: 10px;
  238. padding: 3px 0;
  239. flex: 1;
  240. }
  241. .dialog-text {
  242. font-size: 14px;
  243. color: #333;
  244. }
  245. </style>