当前位置:   article > 正文

uniapp 封装弹窗组件(popup ,可以修改弹窗按钮名,显示内容)_uniapp uni-popup 封装

uniapp uni-popup 封装

 项目中使用了多个弹窗,且内容不一样,于是重新进行封装(避免后期样式大改)

其中:图片,文字 按钮名 功能 都可以自定义(父子相互传值)   

注意:按钮可以不传 默认是 “确认”,"取消"

项目结构↓

 ↓组件代码

  1. <template>
  2. <view>
  3. <uni-popup ref="popup" background-color="#fff">
  4. <view class="popConfig">
  5. <view><img :src="iconUrl" alt=""></view>
  6. <view class="popConfig-txt">
  7. <view class="popConfig-title">{{popName}}</view>
  8. <view class="popConfig-des">{{popDes}}</view>
  9. </view>
  10. <view class="popConfig-btn" v-if="popBtn">
  11. <button @click="operate">{{popBtn[0].name}}</button>
  12. <button @click="close">{{popBtn[1].name}}</button>
  13. </view>
  14. <view class="popConfig-btn" v-else>
  15. <button @click="operate">确认</button>
  16. <button @click="close">取消</button>
  17. </view>
  18. </view>
  19. </uni-popup>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. props: {
  25. showPop: {
  26. type: Boolean,
  27. required: true
  28. },
  29. iconUrl: {
  30. type: String,
  31. required: true
  32. },
  33. popName: {
  34. type: String,
  35. required: true
  36. },
  37. popDes: {
  38. type: String,
  39. required: true
  40. },
  41. popBtn: { // [{name:'确认'},{name:'取消'}]
  42. type: Array,
  43. required: false
  44. }
  45. },
  46. data() {
  47. return {};
  48. },
  49. methods: {
  50. close() {
  51. this.$emit("changeShowPop", false)
  52. },
  53. operate(){
  54. this.$emit("firstBtnOperate","")
  55. }
  56. },
  57. watch: {
  58. 'showPop': {
  59. handler(newVal, oldVal) {
  60. // console.log(111)
  61. if (this.showPop == true) {
  62. this.$nextTick(() => {
  63. this.$refs.popup.open()
  64. })
  65. } else {
  66. this.$nextTick(() => {
  67. this.$refs.popup.close()
  68. })
  69. }
  70. },
  71. deep: true,
  72. immediate: true
  73. }
  74. },
  75. }
  76. </script>
  77. <style lang="scss" scoped>
  78. .popConfig {
  79. background-color: white;
  80. box-sizing: border-box;
  81. padding: 50rpx;
  82. width: 90vw;
  83. min-height: 600rpx;
  84. border-radius: 15rpx;
  85. display: flex;
  86. flex-direction: column;
  87. justify-content: space-between;
  88. align-items: center;
  89. view {
  90. img {
  91. margin-top: 30rpx;
  92. width: 150rpx;
  93. height: 150rpx;
  94. }
  95. }
  96. .popConfig-txt {
  97. text-align: center;
  98. .popConfig-title {
  99. font-size: 40rpx;
  100. color: #3b385aff;
  101. }
  102. .popConfig-des {
  103. font-size: 35rpx;
  104. color: #3b385aff;
  105. }
  106. }
  107. .popConfig-btn {
  108. display: flex;
  109. flex-direction: row;
  110. width: 100%;
  111. justify-content: space-between;
  112. button {
  113. width: 45%;
  114. height: 80rpx;
  115. line-height: 80rpx;
  116. background-color: #5861d0ff;
  117. color: white;
  118. border-radius: 40rpx;
  119. }
  120. button:nth-last-child(1) {
  121. background-color: white;
  122. color: #5861d0;
  123. border: 2rpx solid #5861d0;
  124. }
  125. }
  126. }
  127. </style>

 具体使用↓

  1. <popUpCom
  2. :showPop = "showPop"
  3. iconUrl='../../../static/img/common/完成.png'
  4. popName='完成'
  5. popDes='请确认是否完成?'
  6. @changeShowPop = "changeShowPop"
  7. @firstBtnOperate = "firstBtnOperate"
  8. >
  9. </popUpCom>

script↓ 

注意:

btnConfig:  可以传可以不传,本案例没传 如果要传可以参照:[{name:'确认'},{name:'取消'}]

showPop 变量控制弹窗是否显示,默认为false

toStart() 方法是点击某个按钮触发 打开弹窗

changeShowPop() :方法是关闭弹窗

firstBtnOperate():方法是点击弹窗中的确认按钮,要触发的事件

  1. <script>
  2. import popUpCom from '@/components/popUpCom/popUpCom'
  3. export default {
  4. data() {
  5. return {
  6. btnConfig:[{name:"确认"},{name:"取消"}],
  7. showPop:false
  8. };
  9. },
  10. components: {
  11. popUpCom
  12. },
  13. methods: {
  14. toStart() {
  15. // this.$refs.popup.open('center')
  16. this.showPop = true
  17. },
  18. changeShowPop(v){
  19. console.log(v)
  20. this.showPop = v
  21. },
  22. firstBtnOperate(){
  23. console.log(666)
  24. }
  25. }
  26. }
  27. </script>

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/784282
推荐阅读
相关标签
  

闽ICP备14008679号