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 H5
        if (state.hideTabBar) {
          wx.showTabBar()
        }
        // #endif
        state.show = false
      },
      showModal (state, data) {
        state = Object.assign(state, data)
        console.log(state)
        state.show = true
      },
      success (state, res) {
        let cb = state.success
        let 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 H5
      if (option.hideTabBar) {
        wx.hideTabBar()
      }
      // #endif

      v.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">
            <view
              v-if="cancelText"
              class="btn"
              :style="{ color: cancelColor, background: cancelBackgroundColor }"
              @click.stop="clickBtn('cancel')"
              >{{ cancelText }}</view
            >
            <view
              class="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('用户点击取消')
          }
        }
      })
    }

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp是一个基于Vue.js的跨平台应用开发框架,它可以让你用一套代码同时构建iOS、Android、Web以及H5应用。实现弹窗分享功能通常是在UniApp中集成社交媒体分享组件,如微信、QQ等常见的分享接口。 以下是使用UniApp实现弹窗分享的基本步骤: 1. 引入依赖:在项目中引入uni-app提供的`uni-share`或类似的社会化分享组件库。你可以通过npm安装,然后在需要的地方import进来。 ```javascript // 在main.js或你想使用的组件里 import { share } from '@vant/weapp/share'; ``` 2. 创建分享配置:根据你要分享的内容类型(文本、图片、链接等),定义分享的数据。例如: ```javascript const shareData = { title: '我的精彩内容', // 分享标题 path: 'https://your-url.com', // 分享链接 desc: '这是我想要分享的文字描述', // 分享描述 imgUrl: 'path/to/image.jpg' // 分享图片URL }; ``` 3. 打开分享窗口:调用`share`方法并传入配置数据和回调函数,当用户完成分享后,回调会被执行。 ```javascript share(shareData).then(() => { console.log('分享成功'); }).catch((error) => { console.error('分享失败', error); }); ``` 4. 弹窗样式和行为:uni-app提供了自定义分享面板的选项,可以通过设置参数调整样式,比如是否显示缩略图、图标等。但大部分情况下默认样式就已经足够使用。 ```javascript share({ data: shareData, successCallback() { // 用户点击了确认分享 }, failCallback() { // 用户取消了分享 } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值