一、创建组件
1、在根目录下自定义一个components文件夹,用来存放自定义的组件。
2、再针对每一个组件创建一个文件夹,用来存放这个组件相关的文件。

3、在指定组件的文件夹中右键->新建Component创建组件。这样创建的目的是在json文件中添加"component": true,将其声明为一个组件。

下面开始例子:

1、组件页面 index.wxml

<!-- 确认框 -->
<!-- 遮罩层 -->
<view class="dialog-overlay-view" style="width: {{ windowWidth }}px; height: {{ windowHeight }}px; display: {{ show ? 'block' : 'none' }};"></view><view class="col-center" style="width: {{ windowWidth }}px; height: {{ windowHeight }}px; display: {{ show ? 'flex' : 'none' }};"><view><view class="dialog-content-view"><view><text class="dialog-content-text">{{ message }}</text></view><view class="operation-view"><view class="operation-col-view" bindtouchend="onCancel"><text class="cancel-text">{{ cancelButtonText }}</text></view><view class="operation-col-view" bindtouchend="onConfirm"><text class="confirm-text">{{ confirmButtonText }}</text></view></view></view></view>
</view>

2、组件样式 index.wxss

/* components/dialog/index.wxss */
.dialog-overlay-view {background-color: #000000;opacity: 0.5;position: fixed;z-index: 10;
}
.col-center {position: fixed;z-index: 11;display: flex;flex-direction: column;justify-content: center;align-items: center;
}
.dialog-content-view {width: 210px;background: #FFFFFF;border-radius: 8px;display: flex;flex-direction: column;justify-content: center;padding: 40px 40px 20px 40px;
}
.dialog-content-text {font-size: 14px;font-family: PingFangSC-Regular, PingFang SC;font-weight: 400;color: #454545;line-height: 20px;
}.operation-view {display: flex;flex-direction: row;justify-content: space-between;margin-top: 30px;
}
.operation-col-view {height: 36px;width: 75px;display: flex;flex-direction: column;justify-content: center;align-items: center;
}.cancel-text {height: 14px;font-size: 14px;font-family: PingFangSC-Regular, PingFang SC;font-weight: 400;color: #999999;line-height: 14px;
}
.confirm-text {height: 14px;font-size: 14px;font-family: PingFangSC-Regular, PingFang SC;font-weight: 400;color: #E63719;line-height: 14px;
}

3、组件json配置 index.json

{"component": true,"usingComponents": {}
}

4、组件页面的js index.js

// components/dialog/index.js
Component({options: {/**styleIsolation 选项从基础库版本 2.6.5 开始支持。它支持以下取值:isolated 表示启用样式隔离,在自定义组件内外,使用 class 指定的样式将不会相互影响(一般情况下的默认值);apply-shared 表示页面 wxss 样式将影响到自定义组件,但自定义组件 wxss 中指定的样式不会影响页面;shared 表示页面 wxss 样式将影响到自定义组件,自定义组件 wxss 中指定的样式也会影响页面和其他设置了 apply-shared 或 shared 的自定义组件。(这个选项在插件中不可用。)*/styleIsolation: 'isolated'},/*** 组件的属性列表*/properties: {cancelButtonText: {type: String,value: '取消'},confirmButtonText: {type: String,value: '确定'},message: {type: String,value: ''},show: {type: Boolean,value: false,},confirmCallback: null,cancelCallback: null,},/*** 组件的初始数据*/data: {windowWidth: 0,windowHeight: 0,},/*** 生命周期函数*/ready: function() {var _this = this;wx.getSystemInfo({success: function(res) {_this.setData({windowWidth: res.windowWidth,windowHeight: res.windowHeight,});}});},/*** 组件的方法列表*/methods: {onConfirm() {if (this.properties.confirmCallback) {this.properties.confirmCallback();}this.setData({ show: false });},onCancel() {if (this.properties.cancelCallback) {this.properties.cancelCallback();}this.setData({ show: false });},}
});

5、组件js dialog.js

const defaultOptions = {show: false,message: '',selector: '#cus-dialog',confirmButtonText: '确认',cancelButtonText: '取消',confirmCallback: null,cancelCallback: null,
};
let currentOptions = Object.assign({}, defaultOptions);
function getContext() {const pages = getCurrentPages();return pages[pages.length - 1];
}
const Dialog = (options) => {options = Object.assign(Object.assign({}, currentOptions), options);const context = options.context || getContext();const dialog = context.selectComponent(options.selector);delete options.context;delete options.selector;if (dialog) {dialog.setData(options);wx.nextTick(() => {dialog.setData({ show: true });});}else {console.warn('未找到 cus-dialog 节点,请确认 selector 及 context 是否正确');}
};
Dialog.confirm = (options) => Dialog(Object.assign({ showCancelButton: true }, options));
export default Dialog;

6、使用方法
需要用到dialog的页面引入dialog组件:

{"usingComponents": {"cus-dialog": "../../components/dialog/index"}
}

页面加入dialog节点:

<cus-dialog id="cus-dialog"/>

在页面的js中弹出dialog窗口:

//引入dialog组件
import Dialog from '../../components/dialog/dialog';//在代码中调用
Dialog.confirm({message: '弹窗内容',selector: '#cus-dialog',confirmCallback: function() {console.log('确认啦');}
});

微信小程序自定义Dialog弹框相关推荐

  1. 微信小程序自定义授权弹框

    微信小程序自定义授权弹框 最近微信获取用户信息的接口有调整,就是这货:wx.getUserInfo(OBJECT),文档描述如下: 此接口有调整,使用该接口将不再出现授权弹窗,请使用 <butt ...

  2. 微信小程序自定义picker弹框组件

    要有遥不可及的梦想,也要有脚踏实地的本事.----------- Grapefruit.Banuit Gang(香柚帮) 柚子写项目遇到了一个问题,就是微信小程序官方文档上面的弹框组件picker,它 ...

  3. html自定义js程序,JS中微信小程序自定义底部弹出框

    实现微信小程序底部弹出框效果,代码分为html,css和js两部分,具体代码详情大家参考下本文. html CSS .commodity_screen { width: 100%; height: 1 ...

  4. 微信小程序之底部弹框预约插件

    代码地址如下: http://www.demodashi.com/demo/13982.html 一.前期准备工作: 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq. ...

  5. 微信小程序——自定义下拉框

    微信小程序本身并没有自带的下拉框组件,我们可以通过view标签自定义一个下拉框.(仅供参考) 目录 文件目录 实现效果 实现逻辑 设置数据 设置默认选项 实现弹出下拉框 提供选项值 设置所选值 源码 ...

  6. 微信小程序 -- 自定义底部弹出框(带动画--滑入滑出)

    实现这么一个功能,点击选项进行选择,效果是从底部弹出选项框(带滑出动画),选择了某项或者点击其他地方,隐藏(带滑出动画).效果图如下: 可适用于任何场景,如普通选项(如图)或者类似商城小程序选择商品属 ...

  7. 微信小程序 自定义showmodal 弹出框

    最近一直忙着修改bug 增加新功能,好久没总结了,也不知道该总结啥,先写一个自定义showmodal框吧 废话少说直接上代码 wxml部分 <view class="mask" ...

  8. 微信小程序之model弹框

    小程序model表达形式一般有两种: 1. 第一种: wxml: <button type="primary" bindtap="click">按钮 ...

  9. 微信小程序自定义复选框

    微信官方给的复选框太丑,想要样式多样需要我们自己定义 先来张效果图 wxml循环生成选框,从js里取数据,根据checked的值,改变标签的样式类 <view class="two { ...

  10. uniapp微信小程序的各种弹框提示(轻提示)

    您直接复制粘贴即可使用不需要做特殊的处理. 如您满意请给莫成尘点个Fabulous 1: 纯文字提示框 uni.showToast({title: '只有文字弹窗',icon: 'none', //如 ...

最新文章

  1. 金融学名词M0, M1, M2, M3, M4, M5, M6
  2. clipse3.2/3.3中指定第三方包(JAR)和类路径(CLASSPATH)的几个方法(转做笔记)
  3. (转载)解决umount: /home: device is busy
  4. cmake 版本 arm_尝试使用CMake构建Qt+Pytorch简单应用
  5. ubuntu 12.04 解压安装jdk
  6. 学习deercao的正则笔记
  7. Entity Framework Core 软删除与查询过滤器
  8. python基础教程(十一)
  9. mysql ==null_mysql = null 问题
  10. Solr 新增、更新、删除索引
  11. HTTP 错误 404.2 - Not Found 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面。...
  12. 注册表系列之恶作剧之吻
  13. DSS与Schedulis的集成安装教程(单机)
  14. DDCTFMulTzor-xoortol的使用
  15. postman 测试上传图片
  16. ACM顽固错误之WA——常见掉坑点
  17. python源码文件的后缀名_Python 源代码程序编译后的文件扩展名为_________。_学小易找答案...
  18. 生成图片带有随机码的验证码
  19. android雪花飘落动画,Android自定义View——从零开始实现雪花飘落效果
  20. 42.(leaflet之家)leaflet实现撒点图(仿echarts)

热门文章

  1. 【C++】图像加载(libpng、FreeImage、stb_image)
  2. Android:Json数据转换成Map
  3. 原生ajax请求流程
  4. Java 设计模式 Builder 模式 链式编程
  5. 支持对抗样本防御的AI加速器架构设计
  6. 单片机蓝牙模块与手机蓝牙通信(3)
  7. c# 图片批量转双层PDF,OFD格式文件
  8. Hi,传说中的《超级搜索术》你看了吗?来看看小编的超级总结吧!
  9. 模拟京东商城登陆HttpRequest
  10. MINIST数据集实践