Vue3自定义组件(二)

1、目的        

        在Vue3自定义组件(一)中,我创建了一个简单的表格组件,只新增了一个表头显示,是最简单的组件实现,现在我想对表格做简单的增删改查,就需要进一步改动我的组件了。

2、实现

(1)首先我要展示具体信息的,那我就要有组件去展示,这里我选用了n-modal模态框组件,修改

CommonTable.vue文件:

<template>
    <h6>{{title}}</h6>
    <n-data-table
            :columns="tableColumns"
            :data="data"
            :pagination="pagination"
            :bordered="false"
    >
    </n-data-table>
    <n-modal
            ref="tableModalRef"
            :show="showModal"
            :mask-closable="false"
            preset="dialog"
            :title="modalTitle"
            positive-text="确认"
            negative-text="取消"
            @close="onCloseModal"
            @positive-click="onPositiveClick"
            @negative-click="onNegativeClick"
    >
        <n-form ref="formRef" :model="row">
            <n-form-item v-for="column in tableModalColumns"  :label="column.label">
                <n-input v-if="column.component=='NInput'" :value="selectData[column.field]" :disabled="!isEdit" :placeholder="column.componentProps.placeholder"/>
            </n-form-item>
        </n-form>
    </n-modal>
</template>
<script setup>
    import {NDataTable,useMessage,NModal,NInput,NForm,NFormItem} from "naive-ui"
    import {basicProps} from './props'
    import {unref,computed,reactive,ref,nextTick} from 'vue'

    const message=useMessage();
    const props = defineProps({
        ...basicProps
    });
    const title = unref(props.tableTitle) || '';
    const tableAction=reactive(props.actionColumns) || {};
    const tableColumns=reactive(props.columns) || [];
    //modal相应属性定义
    const showModal=ref(false);
    const modalTitle=unref(props.modalTitle) || '';
    const tableModalColumns=reactive(props.modalColumn) || [];

    //modal是否可编辑
    const isEdit=ref( false);
    //选中的信息
    const selectData=reactive({});
    const modalColums=unref(props.columns.filter((item)=>{
        return item.key!="action";
    })) || [];
    function onPositiveClick(){
        message.info("确认");
        showModal.value=false;

    }
    function onNegativeClick(){
        message.info("取消");
        showModal.value=false;

    }
    tableColumns.forEach(( item,index)=>{
        if(item.key=="action"){
            return;
        }
        if(index==tableColumns.length-1){
            tableColumns.push(props.actionColumns)
        }
    });
    function openModal(row,eidtFlag){
        message.info("打开编辑页面");
        if(eidtFlag){
            isEdit.value=eidtFlag;
        }else{
            isEdit.value=false;
        }
        showModal.value=true;
        Object.assign(selectData,row)
    }
    function onCloseModal(row){
        message.info("关闭页面");
        showModal.value=false;
    }


    //导出方法到外部使用
    defineExpose({
        openModal,
    })

</script>

同时还修改了组件中的props.ts文件,添加了几个参数:

import { NDataTable } from 'naive-ui';
export const basicProps = {
    ...NDataTable.props, // 这里继承原 UI 组件的 props
    //表格头名称
    tableTitle: {
        type: String,
        default: null,
    },
    //操作按钮信息
    actionColumns:{
        type: Object,
        default:{}
    },
    //编辑信息时显示头名称
    modalTitle: {
        type: String,
        default: null,
    },
    //操作数据类型
    operateType: {
        type: String,
        default: "",
    },
    //modal展示的columns
    modalColumn: {
        type: Array,
        default: [],
    },
}

(2)组件中我新增了一个modalColumn参数,这个是设置modal展示哪些字段的:

引用自定组件时,可以创建一个modalColumn.ts,然后设置展现信息:

import {NInput,} from 'naive-ui'

export const modalColumn=[
    {
        field: 'userAcc',
        component: 'NInput',
        label: '用户账号',
        componentProps: {
            placeholder: '请输入用户账号',
        },
        rules: [{ required: true, message: '请输入用户账号', trigger: ['blur'] }],
    },
    {
        field: 'userAcc',
        component: 'NInput',
        label: '用户名',
        componentProps: {
            placeholder: '请输入用户名',
        },
        rules: [{ required: true, message: '请输入用户名', trigger: ['blur'] }],
    },
    {
        field: 'createTime',
        component: 'NInput',
        label: '创建时间',
        componentProps: {
            placeholder: '请输入创建时间',
        },
        rules: [{ required: true, message: '请输入创建时间', trigger: ['blur'] }],
    },
    {
        field: 'lockFlag',
        component: 'NInput',
        label: '是否禁用',
        componentProps: {
            placeholder: '请输入是否禁用',
        },
        rules: [{ required: true, message: '请输入是否禁用', trigger: ['blur'] }],
    },
]

3、实现效果

这样就稍微把之前的组件完善了一下,但是也只是简单调整,后面继续优化它。 

踩过的坑:

(1)不知道如何展示n-modal的字段,因为不知道如何关联选中的值和展示的字段,最根本的原因是不知道如何动态根据一个key获取对象的值,最后才知道可以用object[key]的方式取值;

最终展示n-modal字段逻辑如下:

 

<n-form-item v-for="column in tableModalColumns"  :label="column.label">
    <n-input v-if="column.component=='NInput'" :value="selectData[column.field]" :disabled="!isEdit" :placeholder="column.componentProps.placeholder"/>
</n-form-item>

(2) n-modal顶部的X按钮不能使用,原因是没有配置对应的方法:

@close="onCloseModal"

(3)点击按钮页面没成功展示:

没有把展示页面方法导出:

//导出方法到外部使用
defineExpose({
    openModal,
})

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3 的自定义组件使用很简单。下面是一个简单的示例: 首先,在你的 Vue 项目中创建一个自定义组件的文件,例如 `MyComponent.vue`。 然后,在该文件中,你需要定义一个 Vue 组件。可以使用 `<template>` 标签来定义组件的 HTML 模板,使用 `<script>` 标签来定义组件的 JavaScript 代码,以及使用 `<style>` 标签来定义组件的 CSS 样式。 例如,`MyComponent.vue` 文件内容如下所示: ```vue <template> <div> <h2>{{ title }}</h2> <p>{{ content }}</p> </div> </template> <script> export default { props: { title: String, content: String, }, } </script> <style scoped> h2 { color: blue; } p { font-size: 16px; } </style> ``` 接下来,在你要使用该自定义组件的地方,例如 `App.vue` 文件中,你需要在模板中引入该组件。使用 `<my-component>` 标签来引用该组件,并传递相应的属性值。 例如,`App.vue` 文件内容如下所示: ```vue <template> <div> <my-component title="Hello" content="Welcome to my custom component!"></my-component> </div> </template> <script> import MyComponent from './MyComponent.vue'; export default { components: { MyComponent, }, } </script> <style> /* 可以在这里定义 App.vue 的样式 */ </style> ``` 现在,你就可以在你的 Vue 应用中使用自定义组件了。当你运行应用时,`MyComponent` 组件将会在页面上渲染出来,并显示相应的属性值。 希望以上内容对你有所帮助!如果你有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值