vue3 封装一个包含el-table的el-dialog组件
这种组件是函数式组件的一种形式,它们是无状态且没有实例的Vue组件。是我们创建的带有表格的对话框组件,你需要将它导入到你的父组件中,并通过。是通过父组件传递进来的,用于展示在表格中的数据。是一个响应式引用,控制对话框是否显示。在Vue 3中,可以通过使用。在这个组件中,我们使用了。来定义组件的逻辑,并通过。语法糖来创建一个包含。属性传递数据和标题。
·
在Vue 3中,可以通过使用<script setup>语法糖来创建一个包含el-table的el-dialog组件,而不使用return。这种组件是函数式组件的一种形式,它们是无状态且没有实例的Vue组件。
以下是一个简单的例子:
<template>
<el-dialog v-model="dialogVisible">
<template #title>
<span class="dialog-title">{{ title }}</span>
</template>
<el-table :data="tableData" style="width: 100%">
<el-table-column prop="date" label="日期" width="180"></el-table-column>
<el-table-column prop="name" label="姓名" width="180"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</el-dialog>
</template>
<script setup>
import { ref } from 'vue';
const props = defineProps({
title: String,
tableData: Array,
});
const dialogVisible = ref(false);
</script>
<style scoped>
.dialog-title {
color: #303133;
font-size: 16px;
font-weight: 500;
}
</style>
在这个组件中,我们使用了<script setup>来定义组件的逻辑,并通过defineProps来定义传入的属性。dialogVisible是一个响应式引用,控制对话框是否显示。tableData是通过父组件传递进来的,用于展示在表格中的数据。
要使用这个组件,你可以在父组件中这样引用它:
<template>
<your-dialog-component :table-data="data" title="用户信息" />
</template>
<script setup>
import YourDialogComponent from './YourDialogComponent.vue';
const data = ref([
{ date: '2001-05-02', name: '王军儿', address: '老北京地道胡同' },
// ...更多数据
]);
</script>
在这个例子中,YourDialogComponent是我们创建的带有表格的对话框组件,你需要将它导入到你的父组件中,并通过table-data和title属性传递数据和标题。
更多推荐


所有评论(0)