[Vue3]封装一个弹窗,A组件和B组件打开时内容不同

1.封装公共弹窗组件(Box.vue),利用具名插槽占位

<template>
    <Teleport to="body">
        <slot name="table"></slot>
        <slot name="inform"></slot>
    </Teleport>
</template>

<script setup lang="ts"></script>
<style scoped></style>

2.分别实现A组件以及B组件

①A组件(Inform.vue)

<template>
    <el-button @click="changeInform()">点击查看信息</el-button>
    <Box>
        <template v-slot:inform>
            <div class="modal" v-show="isShow">
                <el-form :model="form" label-width="auto" style="max-width: 300px">
                    <el-form-item label="姓名">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="电话">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="性别">
                        <el-select v-model="form.sex" placeholder="请选择你的性别">
                            <el-option label="男" value="man"></el-option>
                            <el-option label="女" value="women"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onsubmit">提交</el-button>
                        <el-button @click="cancel">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </template>
    </Box>
</template>
<script setup lang="ts" name="Inform">
import Box from "./Box.vue";
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
let isShow = ref(false);
let form = reactive({
    name: "",
    phone: "",
    sex: "",
});
function changeInform() {
    if (!isShow.value) {
        isShow.value = true;
    } else {
        isShow.value = false;
    }
}
function onsubmit() {
    ElMessage("提交");
}
function cancel() {
    isShow.value = false;
}

</script>
<style scoped>
.modal {
    padding: 3px;
    background-color: pink;
    border: 1px solid gray;
    border-radius: 10px;
    position: fixed;
    top: 50px;
    left: 50%;
}
.el-form {
    padding: 5px;
}
.el-form-item .el-button {
    margin-left: 18%;
}
</style>

②B组件(Table.vue),以下直接使用ElementPlus表格内容

<template>
    <el-button @click="changeTable">点击查看表格</el-button>
    <Box>
        <template v-slot:table>
            <div class="table" v-show="isShow">
                <el-table :data="tableData" style="width: 100%">
                    <el-table-column prop="date" label="Date" width="180" />
                    <el-table-column prop="name" label="Name" width="180" />
                    <el-table-column prop="address" label="Address" />
                </el-table>
            </div>
        </template>
    </Box>
</template>
<script setup lang="ts" name="Table">
import { ref, reactive } from "vue";
import Box from "./Box.vue";
let isShow = ref(false);
let tableData = reactive([
    {
        date: "2016-05-03",
        name: "Tom",
        address: "No. 189, Grove St, Los Angeles",
    },
    {
        date: "2016-05-02",
        name: "Tom",
        address: "No. 189, Grove St, Los Angeles",
    },
    {
        date: "2016-05-04",
        name: "Tom",
        address: "No. 189, Grove St, Los Angeles",
    },
    {
        date: "2016-05-01",
        name: "Tom",
        address: "No. 189, Grove St, Los Angeles",
    },
]);
function changeTable() {
    if (!isShow.value) {
        isShow.value = true;
    } else {
        isShow.value = false;
    }
}
</script>
<style scoped>
.table {
    width: 500px;
}
</style>

简单实现效果:(主要是自己离职面试简单练手)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3是一款流行的JavaScript框架,它在构建用户界面方面非常强大。以下是一个示例代码,用于封装一个基本的弹窗组件: ```javascript <template> <div> <button @click="showModal">打开弹窗</button> <div v-if="isOpen" class="modal"> <div class="modal-content"> <slot></slot> <button @click="closeModal">关闭弹窗</button> </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { name: 'Modal', setup() { const isOpen = ref(false); const showModal = () => { isOpen.value = true; }; const closeModal = () => { isOpen.value = false; }; return { isOpen, showModal, closeModal }; }, }; </script> <style scoped> .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .modal-content { background-color: white; padding: 20px; } button { margin-top: 10px; } </style> ``` 这个弹窗组件包含了一个按钮和一个模态框。当点击按钮,模态框会显示出来,并且渲染出插槽内容。关闭按钮可以用于关闭模态框。在模态框外部点击也可以关闭模态框。 这个组件使用Vue3的Composition API来定义逻辑。通过ref函数创建一个响应式引用isOpen,用于跟踪模态框的开启和关闭状态。showModal方法用于打开模态框,closeModal方法用于关闭模态框。使用slot插槽来动态渲染弹窗内容。 在样式上,将模态框设置为固定定位,并使用背景色来实现半透明遮罩效果。模态框内容使用白色背景并设置内边距。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值