vue3.2 封装card组件

一、封装组件 

<template>
  <van-card centered @click="$emit('cardClick', item)" v-for="(item, index) in data" :key="index" class="v-card">
    <template #title>
      <view class="top-style">
        <view class="name-style">{{ item[field.createdName] }}{{ item[field.title] ? item[field.title] : business }}
        </view>
        <view class="time-style" style="color: #707070;">{{ item[field.createTime] }}</view>
      </view>
    </template>
    <template #desc>
      <view class="desc-style">
        <view v-for="(col, k) in columns" :key="k" class="desc-center-style">
          <view style="width: 30%; color: #707070;">{{ col.title }}</view>
          <view style="width: 70%; color: #474747;">
            <view class="content-hide">{{ customValue(item, col) }}</view>
          </view>
        </view>
        <view class="desc-center-style" style="margin-bottom: 2vw" v-if="currentNode">
          <view style="width: 30%; color: #707070;">当前节点</view>
          <view style="width: 70%; color: #188ffd;padding-left: 7%">
            <view class="content-hide">{{ nodePersonHandle(item) }}</view>
          </view>
          <view :class="{
                    'status-style-green': currentStatusHandle(item.status) === '流转完成',
                    'status-style-red': currentStatusHandle(item.status) === '流程关闭',
                    'status-style': true}">{{ currentStatusHandle(item.status) }}
          </view>
        </view>
      </view>
    </template>
  </van-card>
</template>
<script setup>
import { defineProps } from "vue";

const props = defineProps({
  columns: {
    type: Array,
    default: () => []
  },
  business: {
    type: String
  },
  currentNode: {
    type: Boolean,
    default: true
  },
  stateHandle: {
    type: Array,
    default: () => []
  },
  field: {
    type: Object,
    default: () => ({
      title: "title",
      createTime: "createdTime",
      createdName: "createdName",
      currentNames: "currentNames",
      status: "status"
    })
  },
  data: {
    type: Array,
    default: () => []
  }
});
// let statusStyle = ''
const customValue = (item, col) => {
  if (col.custom) {
    return col.custom(item[col.key]);
  } else {
    return item[col.key] ? item[col.key] : "无";
  }
};

const currentStatusHandle = (e) => {
  if (props.stateHandle.length > 0) {
    for (let i = 0; i < props.stateHandle.length; i++) {
        if(props.stateHandle[i].key == e){
          return props.stateHandle[i].value; // 输出当前状态
        }
    }
  }
};

const nodePersonHandle = (item) => {
  let nodeValue = Object.prototype.toString.call(item[props.field.currentNames]).slice(8, -1);
  switch (nodeValue) {
    case "Array":
      return item[props.field.currentNames].length > 0 ? (item[props.field.currentNames].toString().length > 8 ? `${item[props.field.currentNames].toString().slice(0, 9)}...` : `${item[props.field.currentNames].toString()}`) : `无`;
    case "String":
      return item[props.field.currentNames].length > 0 ? (item[props.field.currentNames].length > 8 ? `${item[props.field.currentNames].slice(0, 9)}...` : `${item[props.field.currentNames]}`) : `无`;
    default:
      return "无";
  }
};
</script>

<style scoped>
.v-card {
    width: 95vw;
    height: auto;
    margin: 2vw auto 2vw auto;
    padding-top: 2vw;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    border-left: #cccccc solid 0.5vw;
    border-top: #cccccc solid 0.4vw;
    border-right: #cccccc solid 0.4vw;
    border-bottom: #cccccc solid 0.7vw;
    border-radius: 0.5vw;
    background-color: #ffffff;
}

.top-style {
    width: 320px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 3px;
}

.name-style {
    font-size: 16px;
    font-weight: bold;
}

.time-style {
    font-size: 14px;
    color: #a0a2a4;
    margin-right: -6px;
}

.desc-style {
    display: flex;
    flex-direction: column;
    font-size: 15px;
    margin-top: 5px;
}

.desc-center-style {
    width: 100%;
    height: 6.5vw;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
}

.status-style,
.status-style-red,
.status-style-green {
    height: 7vw;
    width: 30%;
    border-radius: 3px;
    padding-top: 1px;
    text-align: center;
}

.status-style {
    border: solid 1px #138af8;
    color: #138af8;
}

.status-style-red {
    border: solid 1px #ef043a;
    color: #ef043a;
}

.status-style-green {
    border: solid 1px #17a41e;
    color: #17a41e;
}

.content-hide {
    white-space: nowrap;
}
</style>

二、引用组件 

<template>
  <VantCardTest business="发起的安全检查"
    :data="transfer.data" @cardClick="clickTo" :stateHandle="stateHandle"
                :columns="undeterminedColumns"></VantCardTest>
</template>
<script setup>
    import VantCardTest from "@/components/**/***/**.vue";
    // 假数据
    const transfer = reactive({
          data: 
                 [{id:1111,
                  // title:'发起的***', // 可自定义每一条数据
                  createdName:"张三",
                  createdTime:"2024-03-06 11:11",
                  currentNames:['张三','张三','张三','张三','张三'],
                  status:0,
                  code:"单号000000000000",
                  zhidongqiLiaohao:"1234567111",
                  liaohao:"liaohao1123",
                },
                {id:2222,
                 // title:'发起的***', // 可自定义每一条数据
                  createdName:"张三",
                  createdTime:"2024-03-06 11:11",
                  currentNames:["张三","李四无","王五","文5b"],
                  status:3,
                  code:"单号000000000000",
                  zhidongqiLiaohao:"1234567111",
                  liaohao:"liaohao1123",
                }
           ]
      })
    const stateHandle = [
      {key: 1, value: '第一步'},
      {key: 2, value: '第二步'},
      {key: 3, value: '流转完成'},
      {key: 4, value: '流程关闭'}
    ]
    const undeterminedColumns = [
      {title: "业务单号", key: "code"},
      {title: "发起人", key: "createdName"},
      {title: "料号", key: "createdName1"},
      // 可用custom自定义显示的内容  
      // {
      //   title: "当前状态", key: "status", custom: (value) => {
      //     switch (value) {
      //       case 0:
      //         return '待指派1'
      //       case 1:
      //         return '待指派2'
      //       case 2:
      //         return '流转完成'
      //     }
      //   }
      // },
    ]
    const clickTo = (e) => {
          console.log("e",e.id);
    };
</script>

根据引用内容,Vue3.2封装一个组件库的过程包括以下步骤: 1. 组件的项目初建: 1-1. 创建项目; 1-2. 在`packages/button`目录下创建`index.js`文件; 1-3. 在`packages/button`目录下创建`index.vue`文件; 1-4. 在`packages`目录下创建`index.js`文件; 1-5. 在`App.vue`中使用组件; 1-6. 配置`package.json`; 1-7. 配置`main.js`和`vite.config.ts`。 2. 组件库之button组件: 2-1. 在`packages/button`目录下创建`index.vue`文件; 2-2. 在需要使用button组件的地方使用该组件。 3. 组件库之input组件: 3-1. 在`packages/input`目录下创建`index.js`文件; 3-2. 在需要使用input组件的地方使用该组件。 4. 发布npm组件库: - 切换npm镜像为淘宝镜像:`npm config set registry https://registry.npm.taobao.org/`; - 登录npm账号:`npm login`; - 执行打包命令:`npm run build`; - 发布组件库:`npm publish`。 在Vue项目的`main.js`文件中,可以引入组件库并使用: ```javascript import "./assets/main.css"; import TUI from "xzl-vue-ui"; const app = createApp(App); app.use(TUI); app.mount("#app"); ``` 通过以上步骤,你可以完成Vue3.2封装组件库的过程,并可以在项目中使用该组件库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue3.2从0-1封装一个组件库 ( 组件项目的创建 - 发布npm - 使用组件库 )](https://blog.csdn.net/weixin_43845137/article/details/127256751)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3.2二次封装antd vue 中的Table组件(基础封装)](https://blog.csdn.net/m0_57004866/article/details/120419733)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值