当前位置: 首页 > news >正文

uniapp实现自定义弹窗组件,支持富文本传入内容

1.首先安装vuex 
通过此命令安装 ​​npm install vuex --save​

 创建initModal.js

import Vuex from 'vuex'
// 自定义弹窗
export default function initModal (v) {// 挂在store到全局Vue原型上v.prototype.$modalStore = new Vuex.Store({state: {show: false,title: '标题',content: '内容',isRichText: false,showCancel: true,cancelText: '取消',cancelColor: '#333333',cancelBackgroundColor: 'rgba(236, 236, 236, 0.39)',confirmText: '确定',confirmColor: '#333333',confirmBackgroundColor: '#FFBB24',success: null},mutations: {hideModal (state) {// 小程序导航条页面控制// #ifndef H5if (state.hideTabBar) {wx.showTabBar()}// #endifstate.show = false},showModal (state, data) {state = Object.assign(state, data)console.log(state)state.show = true},success (state, res) {let cb = state.successlet resObj = {cancel: false,confirm: false}res == 'confirm' ? (resObj.confirm = true) : (resObj.cancel = true)cb && cb(resObj)}}})v.prototype.$showModal = function (option) {if (typeof option === 'object') {// #ifndef H5if (option.hideTabBar) {wx.hideTabBar()}// #endifv.prototype.$modalStore.commit('showModal', option)} else {throw '配置项必须为对象传入的值为:' + typeof option}}
}

3.showModal.vue
组件实现,我做了个判断,如果传入的cancelText是空字符串,则只显示确认键。 

<template><!-- 自定义弹窗 --><view class="_showModal" v-show="show"><view class="_shade"></view><view class="_modalBox"><view class="_modal"><slot name="title"><view class="title" v-show="title">{{ title }}</view></slot><slot name="content"><view class="content" v-if="isRichText"><rich-text :nodes="content"></rich-text></view><view class="content" v-else>{{ content }}</view></slot><slot name="btn"><view class="btnbox"><viewv-if="cancelText"class="btn":style="{ color: cancelColor, background: cancelBackgroundColor }"@click.stop="clickBtn('cancel')">{{ cancelText }}</view><viewclass="btn":style="{color: confirmColor,background: confirmBackgroundColor}"@click.stop="clickBtn('confirm')">{{ confirmText }}</view></view></slot></view></view></view>
</template><script>
export default {name: 'show-modal',computed: {show () {return this.$modalStore.state.show},title () {return this.$modalStore.state.title},content () {return this.$modalStore.state.content},isRichText () {return this.$modalStore.state.isRichText},showCancel () {return this.$modalStore.state.showCancel},cancelText () {return this.$modalStore.state.cancelText},cancelColor () {return this.$modalStore.state.cancelColor},cancelBackgroundColor () {return this.$modalStore.state.cancelBackgroundColor},confirmText () {return this.$modalStore.state.confirmText},confirmColor () {return this.$modalStore.state.confirmColor},confirmBackgroundColor () {return this.$modalStore.state.confirmBackgroundColor}},methods: {closeModal () {this.$modalStore.commit('hideModal')},clickBtn (res) {this.$modalStore.commit('hideModal')this.$modalStore.commit('success', res)}},beforeDestroy () {this.$modalStore.commit('hideModal')},data () {return {}}
}
</script><style lang="scss" scoped>
._showModal {position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 10000;._shade {width: 100%;height: 100%;position: absolute;top: 0;left: 0;background: #000;opacity: 0.6;z-index: 11000;}._modalBox {width: 100%;height: 100%;position: absolute;top: 0;left: 0;z-index: 12000;display: flex;justify-content: center;align-items: center;._modal {flex: none;width: 345px;//   min-height: 256px;background: #fff;border-radius: 12px;.title {text-align: center;font-size: 18px;font-family: Source Han Sans CN;font-weight: bold;color: #333333;margin-top: 20px;}.content {min-height: 60px;width: 284px;margin: 20px auto;margin-bottom: 30px;font-size: 32rpx;font-family: Source Han Sans CN;font-weight: 500;color: #333333;display: flex;justify-content: center;align-items: center;letter-spacing: 2px;line-height: 40rpx;}.btnbox {display: flex;justify-content: center;// padding-top: 10px;flex-direction: row;.btn {width: 100px;height: 40px;border-radius: 12px;display: flex;justify-content: center;align-items: center;font-family: Source Han Sans CN;font-weight: 500;font-size: 16px;margin: -5px 30px 20px 30px;}}}}
}
</style>

4、在main.js挂载vuex和showModal 

import Vuex from 'vuex'
import initModal from '@/utils/initModal.js'
import ShowModal from '@/components/showModal.vue'initModal(Vue)Vue.use(Vuex)
Vue.component('ShowModal', ShowModal)

5.使用方式  、 在h5 会报错 所有 还是引入一下 小程序正常

 <ShowModal></ShowModal>import ShowModal from '@/components/showModal'components: {ShowModal},confirmBill () {this.$showModal({title: '提示',content: `<div style="font-size:14px">确认该订单吗?<br/> 应付账单<span style="color:red">¥607.5</span><br/> 还款日期:2024-05-02 14:26:52</div>`,isRichText: true,cancelText: '取消', //传入空值表示只显示确认按钮,此代码不能省略confirmText: '确认',success (res) {if (res.confirm) {console.log('用户点击确定')} else if (res.cancel) {console.log('用户点击取消')}}})}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Linux:Linux环境基础开发工具使用
  • DIAdem 与 LabVIEW
  • 【数据结构篇】~顺序表
  • Golang | Leetcode Golang题解之第336题回文对
  • 分布式锁实现方案--redis、zookeeper、mysql
  • Java从zip文件中读取指定的csv文件使用EasyExcel解析出现流关闭异常Stream closed
  • 【常见算法题】斐波那契数列(矩阵快速幂)
  • 歌曲爬虫下载
  • java多线程(初阶)
  • 【Godot4自学手册】第四十五节用着色器(shader)制作水中效果
  • linux C语言remove函数及相关函数
  • 如何选择较为安全的第三方依赖版本?
  • [C++][opencv]基于opencv实现photoshop算法可选颜色调整
  • 微前端架构下的应用版本回退策略与实践
  • C语言 | Leetcode C语言题解之第341题扁平化嵌套列表迭代器
  • [译]Python中的类属性与实例属性的区别
  • 《深入 React 技术栈》
  • 2017届校招提前批面试回顾
  • github从入门到放弃(1)
  • If…else
  • Magento 1.x 中文订单打印乱码
  • nginx 负载服务器优化
  • PHP 小技巧
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • V4L2视频输入框架概述
  • vuex 学习笔记 01
  • Wamp集成环境 添加PHP的新版本
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 阿里云Kubernetes容器服务上体验Knative
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 给Prometheus造假数据的方法
  • 给新手的新浪微博 SDK 集成教程【一】
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 聊聊directory traversal attack
  • 爬虫进阶 -- 神级程序员:让你的爬虫就像人类的用户行为!
  • 设计模式(12)迭代器模式(讲解+应用)
  • 无服务器化是企业 IT 架构的未来吗?
  • 在Unity中实现一个简单的消息管理器
  • 责任链模式的两种实现
  • 做一名精致的JavaScripter 01:JavaScript简介
  • 【云吞铺子】性能抖动剖析(二)
  • 组复制官方翻译九、Group Replication Technical Details
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​zookeeper集群配置与启动
  • ​决定德拉瓦州地区版图的关键历史事件
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • %@ page import=%的用法
  • (4)事件处理——(7)简单事件(Simple events)
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (Note)C++中的继承方式
  • (TOJ2804)Even? Odd?
  • (二)Eureka服务搭建,服务注册,服务发现
  • (二)测试工具
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517