首页 > 编程语言 >(uniapp)小程序实现自定义弹框,自定义样式showmodal

(uniapp)小程序实现自定义弹框,自定义样式showmodal

时间:2023-11-20 12:00:34浏览次数:37  
标签:uniapp return center 自定义 程序实现 100% modalStore state font

在components里新建自定义弹框组件——modal.vue

<template>
    <!-- 自定义弹窗 -->
    <view class="_showModal" v-show="show">
        <view class="_shade"></view>
        <view class="_modalBox">
            <view class="_modal">
                <image src="../static/upgrade.png" mode="widthFix" class="bg-upgrade"></image>
                <slot name="title">
                    <view class="title">{{'发现新版本'+title+',是否重启应用'}}</view>
                </slot>
                <view class="content">本次版本更新内容</view>
                <view v-for="(item,index) in content" :key="index">
                    <slot name="content">
                        <view class="content">{{(index+1)+'、'+item}}</view>
                    </slot>
                </view>
                <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.mshow;
            },
            title() {
                return this.$modalStore.state.mtitle;
            },
            content() {
                return this.$modalStore.state.mcontent;
            },
            showCancel() {
                return this.$modalStore.state.mshowCancel;
            },
            cancelText() {
                return this.$modalStore.state.mcancelText;
            },
            cancelColor() {
                return this.$modalStore.state.mcancelColor;
            },
            cancelBackgroundColor() {
                return this.$modalStore.state.mcancelBackgroundColor;
            },
            confirmText() {
                return this.$modalStore.state.mconfirmText;
            },
            confirmColor() {
                return this.$modalStore.state.mconfirmColor;
            },
            confirmBackgroundColor() {
                return this.$modalStore.state.mconfirmBackgroundColor;
            }
        },
        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: .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;
                height: auto;
                background: #fff;
                border-radius: 12px;
                position: relative;
                padding: 60rpx 0 30rpx 0;

                .bg-upgrade {
                    position: absolute;
                    top: -100rpx;
                    left: 0;
                    right: 0;
                    margin: auto;
                    width: 60%;
                }

                .title {
                    text-align: center;
                    font-size: 18px;
                    font-family: Source Han Sans CN;
                    font-weight: bold;
                    color: #333333;
                    margin: 70px 0 20px 0;
                }

                .content {
                    width: 80%;
                    margin: 20rpx auto;
                    font-size: 14px;
                    font-family: Source Han Sans CN;
                    color: #898989;
                    display: flex;
                    // justify-content: center;
                    align-items: center;
                    word-break: break-all;
                }

                .btnbox {
                    display: flex;
                    justify-content: center;
                    // padding-top: 10px;
                    flex-direction: row;

                    .btn {
                        width: 100%;
                        height: 80rpx;
                        border-radius: 80rpx;
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        font-family: Source Han Sans CN;
                        font-weight: 500;
                        font-size: 16px;
                        margin: 30rpx 30rpx 20rpx 30rpx;
                    }
                }
            }
        }

    }
</style>

在utils里新建modal.js

import Vuex from 'vuex'
// 自定义弹窗
export default function initModal(v) {
    // 挂在store到全局Vue原型上
    v.prototype.$modalStore = new Vuex.Store({
        state: {
            mshow: false,
            mtitle: "",
            mcontent: [],
            mshowCancel: true,
            mcancelText: "取消",
            mcancelColor: "#333333",
            mcancelBackgroundColor: "rgba(236, 236, 236, 0.39)",
            mconfirmText: "确定",
            mconfirmColor: "#333333",
            mconfirmBackgroundColor: "#FFBB24",
            msuccess: null,
        },
        mutations: {
            hideModal(state) {
                // 小程序导航条页面控制
                // #ifndef H5
                if (state.mhideTabBar) {
                    wx.showTabBar();
                }
                // #endif
                state.mshow = false
            },
            showModal(state, data) {
                state = Object.assign(state, data)
                console.log(state);
                state.mshow = true
            },
            success(state, res) {
                let cb = state.msuccess
                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;
        }
    }
}

在项目根目录vue.config.js(没有就新建)文件插入下面代码

module.exports = {
    chainWebpack: config => {
        config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {
            const compile = options.compiler.compile
            options.compiler.compile = (template, ...args) => {
                if (args[0].resourcePath.match(/^pages/)) {
                    template = template.replace(/[\s\S]+?<[\d\D]+?>/, _ => `${_}
                    <updagrade ref="updagrade" />`)
                }
                return compile(template, ...args)
            }
            return options
        })
    }
}

在main.js文件里全局引入

import modaljs from './utils/modal.js'
import modalvue from '@/components/modal.vue'
modaljs(Vue)
Vue.component('modal',modalvue)

 

标签:uniapp,return,center,自定义,程序实现,100%,modalStore,state,font
From: https://www.cnblogs.com/itsone/p/17843627.html

相关文章

  • 使用PIO自定义每一个格子的属性和值,完全DIY--Excel,不整齐也可以实现
    常规表格样式的Excel导出,有一种不是常规表格样式的Excel导出,比如如下这种怎么办快速的excel框架API肯定不支持这种所以我们需要自定义格子的内容privateCellStylegetCellStyle2(XSSFWorkbookworkbook,intx){Fontfont2=workbook.createFont();......
  • UNIAPP 钉钉微应用调试 PC端移动端调试钉钉微应用H5
    https://open.dingtalk.com/document/resourcedownload/micro-application-four-terminal-debugging-tool-web-edition 流程可以参考钉钉文档https://open-dev.dingtalk.com 钉钉开放平台登录      在项目的template.h5.html中的<head>里塞入<scriptsrc="https:......
  • uniapp使用uni-grid时出现BUG第二次进入少一列
    在使用uniapp的uni-grid组件时,出现了一个bug,第一次进入页面是是显示正常的,第二次进入就会发送少一列的情况第一次进入时显示第二次进入时显示在网上找了半天没啥解决办法,最后是在dcloud社区中找到解决方法:1.把最外层的view固定宽度2.去掉边框因为我这边是需要进行对不同设......
  • 自定义Vue脚手架
    创建一个vue项目,项目名叫custom选择最下面的自定义脚手架选择你需要的配置选择vue版本询问是否采用历史模式,默认是hash模式,history模式需要后端进行额外配置选择CSS预处理器选择eslint规范,这里选择的是无分号的规范选择什么时候进行校验,这里选择的是保存时进行校验......
  • 快手抖音同城自动评论脚本,推荐页支持,自定义评论内容,按键精灵开源版!
    之前给客户定制的一个支持快手也抖音的自动同城评论脚本,可以原理很简单,自动上划视频,然后自动留下一个评论,评论内容通过语法分割,实现每次评论的内容都不一样,好了下面是UI和代码。ui:  界面代码:=====================================================界面1:{请在下面设置......
  • 支持抖音快手的直播间刷屏脚本,自定义话术快速,新用户欢迎,按键精灵脚本开源
    用按键精灵之前给客户开发的脚本,功能我这边都设计好了,比如话术速度还有毫秒都可以自定义设置的,还支持虚拟欢迎等功能,还有一直点赞等功能,用按键精灵开发的,我现在拿着也用不了,就直接把源码开源出来。uii界面: 脚本代码:=======================================================......
  • Nginx自定义日志中时间格式
    背景工作需要对接内部的日志中台,对日志打印有固定的格式要求,为了使Nginx的access日志也能被采集,需要对日志格式进行自定义,要求日志格式为:yyyy-MM-ddHH:mm:ss.SSSLOG_LEVELLOG_MSG--->时间格式+打印级别+业务日志如:23-11-1817:34:23.738DEBUGmonitor-7org.apach......
  • Vue的自定义指令
    在使用自定义指令的标签写入v-'自定义指令名'<template><divclass="box"v-loading="loading"></div></template>exportdefault{data(){return{loading:true}},} 公共配置,写在如main.js的公共js中Vue.di......
  • uniapp脚手架中vue3项目配置`@`,并且在vscode中有提示
    uniapp脚手架中vue3项目配置@,并且在vscode中有提示在vite.config.js中配置一下代码import{defineConfig}from"vite";importunifrom"@dcloudio/vite-plugin-uni";import{resolve}from"path";//https://vitejs.dev/config/exportdefaultdefine......
  • javascript 自定义分页组件
    仿boostrap前端分页组件的实现一 写一个前端自定义分页组件,需要考虑以下问题  /*     需要一个<ul id="pagination"></ul>标签   total; // 总数据的数量   pageSize; // 一页显示数量   pageIndex; // 当前页   */ 二实现细节编写html......