首页 > 热点 > 微信 > 正文

微信小程序封装自定义弹窗的实现代码

2024-07-22 01:18:34
字体:
来源:转载
供稿:网友

 最近在做小程序的登录,需要同时获取用户手机号和头像昵称等信息,但是小程序又不支持单个接口同时获取两种数据,因此想到自定义一个弹窗,通过弹窗按钮触发获取手机号事件。记录一下。

具体代码如下:

业务代码中:

  在业务代码中引入dialog组件即可

<dialog visible="{{dialogVisible}}" showFooter="{{footerVisible}}" title="测试一下">    <view class='dialog-body' slot="dialog-body">      <view class='dialog-content'>申请获取你微信绑定的手机号</view>      <view class='dialog-footer' slot="dialog-footer">        <button class='cancel-btn' bindtap="close">取消</button>        <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class='confirm-btn'>授权</button>      </view>    </view>  </dialog>

dialog组件:

component下面新建dialog。注意是 component 不是 page ,因为要作为组件引入到页面中

  dialog.wxml:

  需要传入四个属性

    visible:是否显示弹窗

    title :标题

    showClose:是否显示右上角关闭按钮

    showFooter:是否显示底部按钮

<!--components/dialog/dialog.wxml--><view class='dialog-custom' wx:if="{{visible}}">  <view class='dialog-mask' bindtap="clickMask"></view>    <view class="dialog-main">      <view class="dialog-container">        <view class='dialog-container__title' wx:if="{{title.length>0}}">          <view class='title-label'>{{ title }}</view>          <view class='title-icon'>            <image wx:if="{{showClose}}" bindtap='close' src='/images/close-btn.png'></image>          </view>        </view>      <view class='dialog-container__body'>        <slot name="dialog-body"></slot>      </view>      <view class='dialog-container__footer' wx:if="{{showFooter}}">        <view class='dialog-container__footer__cancel' bindtap="close">取消</view>        <view class='dialog-container__footer__confirm' bindtap='confirm'>确定</view>      </view>    </view>  </view></view>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表