封装表格和标签组件
App.vue

<template> <div class="table-case"> <MyTable :data="goods"> <template #TableHead> <tr> <th>编号</th> <th>名称</th> <th>图片</th> <th width="100px">标签</th> </tr> </template> <template #TableBody="{item,index}"> <tr> <td>{{ index+1 }}</td> <td>{{ item.name }}</td> <td> <img :src="item.picture" /> </td> <td> <MyTagVue :tag.sync="item.tag"> <template> {{ item.tag }} </template> </MyTagVue> </td> </tr> </template> </MyTable> </div> </template> <script> import MyTable from './components/MyTable.vue'; import MyTagVue from './components/MyTag.vue'; export default { name: 'TableCase', components: { MyTable,MyTagVue }, data() { return { goods: [ { id: 101, picture: 'https://yanxuan-item.nosdn.127.net/f8c37ffa41ab1eb84bff499e1f6acfc7.jpg', name: '梨皮朱泥三绝清代小品壶经典款紫砂壶', tag: '茶具', }, { id: 102, picture: 'https://yanxuan-item.nosdn.127.net/221317c85274a188174352474b859d7b.jpg', name: '全防水HABU旋钮牛皮户外徒步鞋山宁泰抗菌', tag: '男鞋', }, { id: 103, picture: 'https://yanxuan-item.nosdn.127.net/cd4b840751ef4f7505c85004f0bebcb5.png', name: '毛茸茸小熊出没,儿童羊羔绒背心73-90cm', tag: '儿童服饰', }, { id: 104, picture: 'https://yanxuan-item.nosdn.127.net/56eb25a38d7a630e76a608a9360eec6b.jpg', name: '基础百搭,儿童套头针织毛衣1-9岁', tag: '儿童服饰', }, ], chageTag:'' } }, } </script> <style lang="less" scoped> .table-case { width: 1000px; margin: 50px auto; img { width: 100px; height: 100px; object-fit: contain; vertical-align: middle; } .my-table { width: 100%; border-spacing: 0; img { width: 100px; height: 100px; object-fit: contain; vertical-align: middle; } th { background: #f5f5f5; border-bottom: 2px solid #069; } td { border-bottom: 1px dashed #ccc; } td, th { text-align: center; padding: 10px; transition: all 0.5s; &.red { color: red; } } .none { height: 100px; line-height: 100px; color: #999; } } } </style>
Mytable.vue

<template> <div class=""> <table class="my-table"> <thead> <slot name="TableHead"></slot> </thead> <tbody v-for="(item,index) in data" :key="item.id"> <slot name="TableBody" :item="item" :index="index"></slot> </tbody> </table> </div> </template> <script> export default { props:{ data:Array }, components:{ } } </script> <style lang="less" scoped> .table-case { width: 1000px; margin: 50px auto; img { width: 100px; height: 100px; object-fit: contain; vertical-align: middle; } .my-table { width: 100%; border-spacing: 0; img { width: 100px; height: 100px; object-fit: contain; vertical-align: middle; } th { background: #f5f5f5; border-bottom: 2px solid #069; } td { border-bottom: 1px dashed #ccc; } td, th { text-align: center; padding: 10px; transition: all 0.5s; &.red { color: red; } } .none { height: 100px; line-height: 100px; color: #999; } } } </style>
MyTag.vue

<template> <div class=""> <div class="my-tag"> <input v-if="isEdit" v-focus @blur="handleChage" class="input" type="text" :placeholder="Tag" /> <div v-else class="text" @dblclick="isEdit=true" > <slot></slot> </div> </div> </div> </template> <script> export default { data(){ return{ isEdit:false, newTag:'' } }, props:{ Tag:String }, methods:{ handleChage(e){ if(e.target.value.trim()=='')return alert('标签内容不能为空') this.$emit("update:tag",e.target.value) this.isEdit=false } } } </script> <style lang="less" scoped> .my-tag { cursor: pointer; .input { appearance: none; outline: none; border: 1px solid #ccc; width: 100px; height: 40px; box-sizing: border-box; padding: 10px; color: #666; &::placeholder { color: #666; } } } </style>
在app.vue里面可以实现表头,表内容的定制
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现