代码附件

jsx 封装弹窗组件

import { createApp, defineComponent } from "vue";
import { styled } from '@styils/vue'
import { ElButton } from "element-plus";

const DivModal = styled('div', {
    position: 'fixed',
    width: '100%',
    height: '100%',
    left: 0,
    top: 0,
    Zindex: 99,
    background: '#00000050',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center'
})

const DivBox = styled('div', {
    minWidth: '30%',
    background: '#fff',
    padding: '10px 0',
    color: '#333',
    borderRadius: '10px',
    boxShadow: '0 0 3px #00000080',
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center'
})

const DivText = styled('div', {
    marginBottom: '1em'
})

const MessageBox = defineComponent({
    props: {
        msg: {
            type: String,
            required: true
        }
    },
    render(ctx) {
        const { $props, $emit } = ctx;

        // jsx 语法
        return (
            <DivModal>
                <DivBox>
                    <DivText>{$props.msg}</DivText>
                    <ElButton type="primary" onClick={ $emit('onClick') }>按钮</ElButton>
                </DivBox>
            </DivModal>
        )
    }
});


export function showMsg(msg, cbFn) {
    const div = document.createElement('div');
    document.body.appendChild(div);

    // 渲染组件到界面上
    const app = createApp(MessageBox, {
        msg,
        onClick() {
            cbFn(() => {
                app.unmount();
                div.remove();
            });
        }
    });
    app.mount(div);
}

函数调用的方式使用

<template>
	<el-button type="primary" @click="onClick">打开弹窗</el-button>
</template>

<script>
import { showMsg } from '/src/utils/message.js';

export default {
	setup() {
		const onClick = () => {
			showMsg('你好', (closeFn) => {
				closeFn();
				console.log('关闭了弹窗');
			});
		};
		return { onClick };
	},
};
</script>
打赏作者
您的打赏是我前进的动力
微信
支付宝
评论

中午好👏🏻,我是 ✍🏻   疯狂 codding 中...

粽子

这有关于前端开发的技术文档和你分享。

相信你可以在这里找到对你有用的知识和教程。

了解更多

目录

  1. 1. 代码附件
  2. 2. jsx 封装弹窗组件
  3. 3. 函数调用的方式使用