vue代码优化

① 对象映射

arduino 复制代码
const statusMap = {
  0: '审核中',
  1: '审核成功', 
  2: '审核失败',
  3: '打回重填'
};
​
// 使用方式
const statusText = statusMap[status];
​

②组件懒加载

xml 复制代码
<template>
    <Components />
</template>
​
<script lang="ts" setup>
    const Components = defineAsyncComponent(async () => import('./components/Components/Components.vue'));
</script>

③计算属性

xml 复制代码
<template>
    <div class="filter-header">
      <div class="status-display" :style="{ color: statusColor }">
        {{ statusText }}
      </div>
</template>
​
<script lang="ts" setup>
import { computed } from 'vue';
    const statusMap = {
      0: '审核中',
      1: '审核成功',
      2: '审核失败',
      3: '打回重填'
    }
​
    const statusColorMap = {
      0: 'var(--el-text-color-secondary)',
      1: 'var(--el-color-success)',
      2: 'var(--el-color-danger)',
      3: 'var(--el-color-warning)'
    }
    
    const statusText = computed(() => {
      return statusMap[props.tableData?.status] || ''
    })
​
    const statusColor = computed(() => {
      return statusColorMap[props.tableData?.status] || ''
    })
</script>
​

④ 单行返回语句

kotlin 复制代码
if (this.previousCsGroupId === csGroupId) return;
if (!value) return;

⑤使用模板字面量进行模版字符串拼接

ini 复制代码
const key = `${prefix}/static`;

⑥过滤filter

php 复制代码
<template>
    <el-table :data="filteredList">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="status" label="状态" />
    </el-table>
</template>
​
<script lang="ts" setup>
    import { ref, computed } from 'vue'
​
    const list = ref([
      {id: 0, name: 'test1', status: 0},
      {id: 1, name: 'test2', status: 1},
      {id: 2, name: 'test3', status: 0},
      {id: 3, name: 'test4', status: 1},
      {id: 4, name: 'test5', status: 3},
      {id: 5, name: 'test6', status: 1},
      {id: 6, name: 'test7', status: 3},
      {id: 7, name: 'test8', status: 0},
      {id: 8, name: 'test9', status: 4}
    ])
​
    const filteredList = computed(() => {
      return list.value.filter(item => item.status === 1)
    })
</script>
​
​

⑦定义枚举

constant.ts

ini 复制代码
// 使用 enum 定义错误码
export enum ErrorCodes {
  PARAM_ERROR = 10090018,
  SUBMIT_ERROR = 10090023,
  EMPTY_MATERIAL = 10090024,
  SUBMITTER_MISMATCH = 10090028
}
​
// 使用 interface 定义错误消息映射
export interface IErrorMessages {
  [key: number]: string
}
​
export const ERR_MSG: IErrorMessages = {
  [ErrorCodes.PARAM_ERROR]: '参数异常',
  [ErrorCodes.SUBMIT_ERROR]: '提交异常',
  [ErrorCodes.EMPTY_MATERIAL]: '提交材料为空',
  [ErrorCodes.SUBMITTER_MISMATCH]: '审核提交人不一致',
};
​
​

弹出提示

xml 复制代码
<script lang="ts" setup>
import { ERR_MSG } from '/constant.ts';
import { computed, defineAsyncComponent, getCurrentInstance, onMounted, reactive, ref } from 'vue';
const { proxy } = getCurrentInstance();
​
const next = async () => {
    ...
    if (res.retInNode !== 0) {
            state.submitErr = ERR_MSG[res.retInNode] || proxy.$i18next.t('参数异常');
    }
}
​
</script>

constant.ts

less 复制代码
// 定义是提审类型
export const IS_EXPEDITED_TYPE = {
  Normal: 0, // 选择不加急
  Urgent: 1, // 选择加急
  RefillUrgent: 2, // 【打回】显示加急
};

使用类型

xml 复制代码
<template>
    <div>
        {{ (state.formValue.expedited === IS_EXPEDITED_TYPE.Normal || speedupRest === SpeedupRest_Time.NoQuota) ? '不加急' : '加急' }}
    </div>
</template>
<script lang="ts" setup>
import { IS_EXPEDITED_TYPE } from '/constant.ts';
</script>
相关推荐
余生H13 小时前
前端技术新闻(WTN-1):React.js & Next.js 爆出 CVSS 10.0 级严重漏洞,历史风险回顾与代码级深度分析
前端·javascript·react.js
1024肥宅13 小时前
JavaScript 原生方法实现:数学与数字处理全解析
前端·javascript·ecmascript 6
烟袅13 小时前
深入理解 JavaScript 内存机制与闭包原理
前端·javascript
烟袅14 小时前
JavaScript 内存三空间协同机制:代码空间、栈空间与堆空间如何联合运行
前端·javascript
lqj_本人14 小时前
DevUI高频组件(Form 组件)深度用法与避坑指南
前端·javascript
live丶14 小时前
从零实现一个低代码 H5 页面编辑器(Vue3 + 拖拽)
前端·vue.js
码界奇点14 小时前
基于Django REST framework与Vue的前后端分离后台管理系统设计与实现
vue.js·后端·python·django·毕业设计·源代码管理
黑臂麒麟14 小时前
华为云 DevUI初体验:如何快速入门项目搭建
前端·ui·华为云·devui
翔云 OCR API14 小时前
企业工商信息查验API-快速核验企业信息-营业执照文字识别接口
前端·数据库·人工智能·python·mysql
小明记账簿_微信小程序14 小时前
js实现页面全屏展示
前端