微信小程序全局自定义弹窗

全局自定义弹窗

微信小程序原有弹窗不满足业务,因此封装一个自定义弹窗

创建自定义组件

创建components-model组件,生成wxml,js,json,scss文件

<!--components/model.wxml-->
<!--
  title 标题
  content 内容
  showCancel 是否显示取消按钮
  cancelText 取消按钮文案
  confirmText: 确认按钮文案
-->
<view class="modal" wx:if="{{ isShow }}" >
    <view class="m-body">
      <view class="m-cont" style="{{customStyle}}">
        <view class="title">{{ title }}</view>
        <text class="text {{ textAlign }}">{{ content }}</text>
        <view class="m-btn" wx:if="{{ showCancel }}">
          <view class="btn btn-1" bind:tap="_cancel">{{ cancelText }}</view>
          <view class="btn btn-2" bind:tap="_confirm">{{ confirmText }}</view>
        </view>
        <view class="m-btn" wx:if="{{ !showCancel }}">
          <view class="btn btn-2 btn-3" bind:tap="_confirm">{{ confirmText }}</view>
        </view>
      </view>
    </view>
</view>
// components/model.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value:'',
    },
    content: {
      type: String,
      value: ''
    },
    showCancel: {
      type: Boolean,
      value: true
    },
    textAlign: {
      type: String,
      value: 'center'
    },
    cancelText: {
      type: String,
      value: '取消'
    },
    confirmText: {
      type: String,
      value: '确定'
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    isShow:true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 确认
    _confirm (e) {
      // 触发组件事件,传递数据
      this.triggerEvent("onConfirm");
      this.triggerEvent("onCancel",false);
    },
    // 取消
    _cancel() {
      this.setData({
        isShow:false
      })
      this.triggerEvent("onCancel",this.data.isShow);
    }
  }
})
/* components/model.wxss */
.modal{
  
  .m-body {
    position: fixed;
    width: 100vw;
    height: 100vh;
    top: 0;
    left: 0;
    background: rgba(51,51,51,0.6);
    z-index: 992;
    .m-cont{
      position: fixed;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 526rpx;
      background: #FFFFFF;
      border-radius: 24rpx;
      padding: 32rpx;
      z-index: 999;
      .title {
        height: 44rpx;
        font-size: 32rpx;
        font-weight: 600;
        color: #333333;
        line-height: 44rpx;
        margin-bottom: 16rpx;
        text-align: center;
      }
      .text{
        display: block;
        font-size: 28rpx;
        color: #333333;
        line-height: 40rpx;
        margin-bottom: 40rpx;
        
      }
      .center {
        text-align: center;
      }
      .left {
        text-align: left;
      }
      .rgiht {
        text-align: right;
      }
      .m-btn {
        display: flex;
        justify-content: space-between;
        .btn {
          width: 248rpx;
          height: 80rpx;
          line-height: 76rpx;
          border-radius: 40rpx;
          font-size: 28rpx;
          text-align: center;
          box-sizing: border-box;
        }
        .btn-1 {
          border: 2rpx solid #CCCCCC;
          color: #818181;
        }
        .btn-2 {
          font-weight: 600;
          color: #333333;
          background: linear-gradient(180deg, #FFDF0F 0%, #FFF336 100%);
        }
        .btn-3{
          width: 100%;
        }
      }
    }
  }
}

注入model组件

小程序app.json,引入组件

"usingComponents": {
    "model": "/components/model/index"
  },

页面使用

// data 数据
 model: {
      title: '温馨提示',
      content: '内容',
      confirmText: '确定',
      cancelText:'取消',
      isShowModel: false
    },
<model wx:if="{{ model.isShowModel }}" content="{{ model.content }}" title="{{ model.title }}" confirmText="{{ model.confirmText }}"bind:onConfirm="onConfirm" bind:onCancel="onCancel"></model>

总结

比较简单的自定义弹窗,没有加显示隐藏的动画效果,页面多次调用先把内容重置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值