• vue3使用tsx自定义弹窗组件


    1.在ts代码中使用css

    我这里使用了@styils/vue,npm install @styils/vue --save-dev,在tsx文件中引入即可:import { styled } from "@styils/vue";

    2.在tsx中初始化组件,创建在src的utils目录中创建messagebox.tsx

    1. import { createApp} from "vue";
    2. // 这里使用了element-plus的组件,请自行引入即可
    3. import { ElButton } from "element-plus";
    4. import { styled } from "@styils/vue";
    5. const DivModal = styled('div', {
    6. position: 'fixed',
    7. width: '100%',
    8. height: '100%',
    9. left: 0,
    10. top: 0,
    11. background: '#00000050',
    12. display: 'flex',
    13. justifyContent: 'center',
    14. alignItems: 'center'
    15. });
    16. const DivBox = styled('div', {
    17. display: 'flex',
    18. minWidth: '25%',
    19. background: '#fff',
    20. padding: '10px 0',
    21. color: '#333',
    22. borderRadius: '10px',
    23. boxShadow: '0 0 3px #00000080',
    24. flexDirection: 'column',
    25. alignItems: 'center'
    26. });
    27. const DivText = styled('div', {
    28. marginBottom: '1em'
    29. });
    30. const Messagebox = {
    31. props: {
    32. msg: {
    33. type: String,
    34. required: true
    35. },
    36. },
    37. render(ctx: any) {
    38. const { $props, $emit } = ctx;
    39. return (
    40. <DivModal class="modal">
    41. <DivBox class="box">
    42. <DivText class="text">{$props.msg}DivText>
    43. <div onClick={$emit('onClick(e)')}>
    44. <ElButton type="primary">确 定ElButton>
    45. div>
    46. DivBox>
    47. DivModal>
    48. );
    49. },
    50. };
    51. export function showMsg(msg: String, isModal: Boolean | null, onClickHandler: Function) {
    52. const div = document.createElement("div");
    53. document.body.appendChild(div);
    54. const app = createApp(Messagebox,
    55. {
    56. msg,
    57. onClick(e: any) {
    58. if (isModal) {
    59. onClickHandler(() => {
    60. app.unmount();
    61. div.remove();
    62. });
    63. } else {
    64. const isButton = e.target.localName === "button" || e.target.innerText === "确定";
    65. if (isButton) {
    66. onClickHandler(() => {
    67. app.unmount();
    68. div.remove();
    69. });
    70. }
    71. }
    72. }
    73. }
    74. );
    75. app.mount(div);
    76. };

    3.在vue中使用该组件

    1. <template>
    2. <el-button type="primary" @click="showTsxMsg">显示tsx封装的弹窗el-button>
    3. template>
    4. <script lang="ts" setup>
    5. import { showMsg } from "@/utils/messagebox";
    6. const showTsxMsg = () => {
    7. showMsg("tsx封装的组件", true, (close: Function) => {
    8. close();
    9. });
    10. };
    11. script>

  • 相关阅读:
    82.(cesium之家)cesium点在3d模型上运动
    Linux Bash Shell 中变量的 5 个易错点
    十六、代码校验(1)
    QT自定义信号,信号emit,信号参数注册
    JavaWeb基础面试题
    【JAVAEE框架】Spring 项目构建流程
    【云原生 | 从零开始学Kubernetes】十四、k8s核心技术-Controller
    关于HTTP/1.1,HTTP/2和HTTP/3的区别与联系
    C#中Redis使用简单教程
    【王道】计算机组成原理第五章中央处理器(五)
  • 原文地址:https://blog.csdn.net/l244112311/article/details/134542432