uni-popup.uvue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <view class="popup-root" v-if="isOpen" v-show="isShow" @click="clickMask">
  3. <view @click.stop>
  4. <slot></slot>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. type CloseCallBack = ()=> void;
  10. let closeCallBack:CloseCallBack = () :void => {};
  11. export default {
  12. emits:["close","clickMask"],
  13. data() {
  14. return {
  15. isShow:false,
  16. isOpen:false
  17. }
  18. },
  19. props: {
  20. maskClick: {
  21. type: Boolean,
  22. default: true
  23. },
  24. },
  25. watch: {
  26. // 设置show = true 时,如果没有 open 需要设置为 open
  27. isShow:{
  28. handler(isShow) {
  29. // console.log("isShow",isShow)
  30. if(isShow && this.isOpen == false){
  31. this.isOpen = true
  32. }
  33. },
  34. immediate:true
  35. },
  36. // 设置isOpen = true 时,如果没有 isShow 需要设置为 isShow
  37. isOpen:{
  38. handler(isOpen) {
  39. // console.log("isOpen",isOpen)
  40. if(isOpen && this.isShow == false){
  41. this.isShow = true
  42. }
  43. },
  44. immediate:true
  45. }
  46. },
  47. methods:{
  48. open(){
  49. // ...funs : CloseCallBack[]
  50. // if(funs.length > 0){
  51. // closeCallBack = funs[0]
  52. // }
  53. this.isOpen = true;
  54. },
  55. clickMask(){
  56. if(this.maskClick == true){
  57. this.$emit('clickMask')
  58. this.close()
  59. }
  60. },
  61. close(): void{
  62. this.isOpen = false;
  63. this.$emit('close')
  64. closeCallBack()
  65. },
  66. hiden(){
  67. this.isShow = false
  68. },
  69. show(){
  70. this.isShow = true
  71. }
  72. }
  73. }
  74. </script>
  75. <style>
  76. .popup-root {
  77. position: fixed;
  78. top: 0;
  79. left: 0;
  80. width: 750rpx;
  81. height: 100%;
  82. flex: 1;
  83. background-color: rgba(0, 0, 0, 0.3);
  84. justify-content: center;
  85. align-items: center;
  86. z-index: 99;
  87. }
  88. </style>