首页 > 其他分享 >Vue 3 后端错误消息处理范例

Vue 3 后端错误消息处理范例

时间:2024-07-13 23:51:33浏览次数:10  
标签:范例 Vue const 错误 value field errors error

1. 错误消息格式

前后端消息传递时,我们可以通过 json 的 errors 字段传递错误信息,一个比较好的格式范例为:

{
  errors: {
    global: ["网络错误"],
    password: ["至少需要一个大写字母", "至少需要八位字符"]
  }
}

errors 中,字段名代表出错位置(如果是输入框的话,对应错误要显示在框下面),内容为一个数组,每个字符串代表一个错误。

2. 处理函数

可以新建一个 composables 文件夹,以存储各个 components 中共用的逻辑,例如错误消息处理。这里在 composables 文件夹中新建一个 error.ts

import { ref, type Ref } from 'vue';

export interface ErrorFields {
  global: string[];
  [key: string]: string[];
}

export function useErrorFields(fields: string[]) {
  const errors: Ref<ErrorFields> = ref({ global: [], ...fields.reduce((acc, field) => ({ ...acc, [field]: [] }), {}) });
  const clearErrors = () => {
    for (const field in errors.value) {
      errors.value[field] = [];
    }
  };
  const hasErrors = (field?: string) => {
    if (field) {
      return errors.value[field].length > 0;
    }
    return Object.values(errors.value).some((field) => field.length > 0);
  };
  const addError = (field: string, message: string) => {
    if (field === '') {
      field = 'global';
    }
    const array = errors.value[field];
    if (!array.includes(message)) {
      array.push(message);
    }
    return array;
  };
  const removeError = (field: string, message?: string) => {
    if (field === '') {
      field = 'global';
    }
    if (message) {
      errors.value[field] = errors.value[field].filter((m) => m !== message);
    } else {
      errors.value[field] = [];
    }
  };
  return { errors, clearErrors, hasErrors, addError, removeError };
}

这里我们就定义了错误类及其处理函数。

3. 组件中的使用

定义的 useErrorFields 工具可以在 component 中这样使用:

<script setup lang="ts">
import axios from 'axios';
import { computed, onMounted, ref, type Ref } from 'vue';
import { useErrorFields } from '@/composables/error';

const { errors, clearErrors, addError, hasErrors } = useErrorFields(['username', 'password']);

const username = ref('');

function onSubmit() {
  const api = axios.create({
    baseURL: import.meta.env.VITE_API_URL,
  });
  api.get("/user/register")
  .catch((error) => {
    if (error.response && error.response.data && error.response.data.errors) {
      errors.value = { ...errors.value, ...error.response.data.errors };
    } else if (error.response) {
      addError('', '未知错误');
    } else {
      addError('', '网络错误');
    }
  })
}
</script>

<template>
  <div
    v-if="hasErrors('global')"
    class="mb-5 rounded-md border-0 shadow-sm ring-1 ring-inset ring-gray-300 dark:ring-gray-500 px-4 py-2"
  >
    <div class="flex text-red-700 dark:text-rose-400 space-x-2 mb-2">
      <p class="text-lg font-semibold">错误</p>
    </div>
    <ul class="flex flex-col font-medium tracking-wide text-sm list-disc pl-6">
      <li v-for="e in errors.global" v-html="e" />
    </ul>
  </div>
  <form>
    <div>
      <label for="username" class="block text-sm font-medium leading-6">
        用户名
        <span class="text-red-700">*</span>
      </label>
      <div class="mt-2">
        <input
          v-model="username"
          @focus="clearErrors"
          id="username"
          name="username"
          type="text"
          autocomplete="username"
          required
          class="block w-full rounded-md border-0 py-1.5 px-3 shadow-sm ring-1 ring-inset focus:ring-2 focus:ring-inset focus:ring-indigo-600 focus:outline-none sm:text-sm sm:leading-6 dark:bg-white/10 dark:ring-white/20"
          :class="{ 'ring-red-500': hasErrors('username'), 'ring-gray-300': !hasErrors('username') }"
        />
      </div>
      <ul class="flex flex-col font-medium tracking-wide text-red-500 text-xs mt-1 ml-1">
        <li v-for="e in errors.username" v-html="e" />
      </ul>
    </div>
    <div>
      <button
        type="submit"
        class="flex w-full justify-center rounded-md px-3 py-1.5 text-sm font-semibold leading-6 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 text-white shadow-sm hover:bg-indigo-500"
        :class="{
          'cursor-default pointer-events-none': hasErrors() || processing,
          'bg-gray-400': hasErrors(),
          'bg-indigo-600': !hasErrors(),
        }"
      >
        注册
      </button>
    </div>
  </form>
</template>

接下来,我们一步步解析以上代码。

3.1 根据后端响应更新错误状态

我们首先使用 useErrorFields 定义了一个错误状态类:

const { errors, clearErrors, addError, hasErrors } = useErrorFields(['username', 'password']);

这时候,错误状态 errors 中可访问三个字段,并将绑定到页面的不同位置:

global: 全局错误 / 无具体位置的错误 => 显示在表格顶端的单独框中

username: 用户名上的错误 => 显示在 username 输入框下方
password: 密码上的错误 => 显示在 password 输入框下方

接下来,我们需要定义提交函数,例如这里使用 axios 进行后端访问,后端地址用环境变量提供:

function onSubmit() {
  const api = axios.create({
    baseURL: import.meta.env.VITE_API_URL,
  });
  api.get("/user/register")
  .catch((error) => {
    if (error.response && error.response.data && error.response.data.errors) {
      errors.value = { ...errors.value, ...error.response.data.errors };
    } else if (error.response) {
      addError('', '未知错误');
    } else {
      addError('', '网络错误');
    }
  })
}

这样,后端返回错误信息时,错误状态会被自动更新。如果出现了网络错误或其他错误,addError类会在 global 字段上增加错误 (使用空字符串为第一个参数,默认添加到 global 字段)。

接下来,将错误状态绑定到页面。

3.2 绑定到输入框

<input
  v-model="username"
  @focus="clearErrors"
  id="username"
  name="username"
  type="text"
  autocomplete="username"
  required
  class="block w-full rounded-md border-0 py-1.5 px-3 shadow-sm ring-1 ring-inset focus:ring-2 focus:ring-inset focus:ring-indigo-600 focus:outline-none sm:text-sm sm:leading-6 dark:bg-white/10 dark:ring-white/20"
  :class="{ 'ring-red-500': hasErrors('username'), 'ring-gray-300': !hasErrors('username') }"
/>

这里主要使用了两个个函数:

clearErrors: 当重新开始进行输入时,清除错误状态中的全部错误。

hasErrors: 当对应位置出现错误时,将输入框边框颜色变为红色。

将错误状态显示在输入框下:

<div>
  <label for="username" class="block text-sm font-medium leading-6">
    用户名
    <span class="text-red-700">*</span>
  </label>
  <div class="mt-2">
    <input
      ...
    />
  </div>
  <ul class="flex flex-col font-medium tracking-wide text-red-500 text-xs mt-1 ml-1">
    <li v-for="e in errors.username" v-html="e" />
  </ul>
</div>

这里我们使用 <li> 标签,使用 errors.username 将对应位置的错误消息依次显示在输入框下。

3.4 全局消息显示在表格顶端

<div
  v-if="hasErrors('global')"
  class="mb-5 rounded-md border-0 shadow-sm ring-1 ring-inset ring-gray-300 dark:ring-gray-500 px-4 py-2"
>
  <div class="flex text-red-700 dark:text-rose-400 space-x-2 mb-2">
    <p class="text-lg font-semibold">错误</p>
  </div>
  <ul class="flex flex-col font-medium tracking-wide text-sm list-disc pl-6">
    <li v-for="e in errors.global" v-html="e" />
  </ul>
</div>
<form>
  ...
</form>

这里使用 hasErrors('global') 来检测是否有全局错误,并在输入表顶端显示。

3.5 提交按钮在有错误时不允许点击

<button
  type="submit"
  class="flex w-full justify-center rounded-md px-3 py-1.5 text-sm font-semibold leading-6 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 text-white shadow-sm hover:bg-indigo-500"
  :class="{
    'cursor-default pointer-events-none': hasErrors(),
    'bg-gray-400': hasErrors(),
    'bg-indigo-600': !hasErrors(),
  }"
>
  注册
</button>

这里使用 hasErrors() 来检测错误状态类中是否有任何错误,并据此启用或禁用按钮。

4. 完整案例

如果你需要一个完整案例,这里有:错误状态处理在用户注册场景的案例,前端开源,详见:Github,你也可以访问 Githubstar.pro 来查看网页的效果(一个 Github 互赞平台,前端按本文方式进行错误处理)。

感谢阅读,如果本文对你有帮助,可以订阅我的博客,我将继续分享前后端全栈开发的相关实用经验。祝你开发愉快

标签:范例,Vue,const,错误,value,field,errors,error
From: https://www.cnblogs.com/techhub/p/18301010/vue-error-handle

相关文章

  • three.js+vue污水处理厂数字孪生平台智慧城市web3d
    案例效果截图如下: 主场景三维逻辑代码如下:<template><divclass="whole"><!--threejs画布--><divid="threejs"ref="threejs"></div><!--污水厂模型加载进度条--><a-progress:stroke-colo......
  • 基于SpringBoot+Vue+uniapp的校园美食交流系统的详细设计和实现(源码+lw+部署文档+讲
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 基于SpringBoot+Vue+uniapp的房屋租售网站的详细设计和实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 基于SpringBoot+Vue+uniapp的蜀都天香酒楼系统的详细设计和实现(源码+lw+部署文档+讲
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 力扣-278. 第一个错误的版本
    1.题目题目地址(278.第一个错误的版本-力扣(LeetCode))https://leetcode.cn/problems/first-bad-version/题目描述你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所......
  • 基于SpringBoot+VueJS+微信小程序技术的图书森林共享小程序设计与实现:7000字论文+源
          博主介绍:硕士研究生,专注于信息化技术领域开发与管理,会使用java、标准c/c++等开发语言,以及毕业项目实战✌    从事基于javaBS架构、CS架构、c/c++编程工作近16年,拥有近12年的管理工作经验,拥有较丰富的技术架构思想、较扎实的技术功底和资深的项目管理经......
  • springboot+vue+mybatis文化遗产管理系统+PPT+论文+讲解+售后
    信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多......
  • ts vue3 自定义指令
    当然,以下是将前面两个步骤汇总到一起的完整实现方案:1.定义指令首先,在src/directives文件夹中创建你的自定义指令文件。例如,v-focus.ts和v-color.ts:v-focus.ts:import{Directive}from'vue';constvFocus:Directive={mounted(el){el.focus();}};ex......
  • vue-cli 搭建的项目 node搞版本兼容 配置写法
    先删除package-lock.json和node_modules再安装 兼容低版本安装方式 npminstall--legacy-peer-deps启动命令npmrunservewindow环境打包命令npmrunbuild:win注意:如果是要跑liunx环境就需要多安装一条命令window环境打包命令npmrunbuild:liunx......
  • pip安装错误:error: externally-managed-environment
    pip安装出错:error:externally-managed-environment×Thisenvironmentisexternallymanaged╰─>ToinstallPythonpackagessystem-wide,tryaptinstallpython3-xyz,wherexyzisthepackageyouaretryingtoinstall.参考:https://stackoverflow.com/q......