vue.js使用table连接数据库字段

admin 104 0
Vue.js作为前端框架,需通过后端API连接数据库实现表格数据展示,通常使用axios发送HTTP请求,后端(如Node.js+Express、Java+Spring Boot等)负责数据库查询(如MySQL、MongoDB),获取字段数据后以JSON格式返回,前端接收数据后,借助表格组件(如Element UI的el-table、Ant Design Table)进行渲染,通过数据绑定将数据库字段映射为表格列,实现动态数据展示,此模式确保前后端分离,保障数据交互安全与前端组件复用性。

Vue.js 中实现表格与数据库字段的动态连接与数据展示

在现代 Web 应用开发中,数据展示是核心需求之一,表格作为结构化数据最常用的展示组件,需要与数据库字段动态关联,实现数据的实时、灵活渲染,Vue.js 凭借其响应式数据绑定和组件化开发优势,能够高效实现表格与数据库字段的连接,本文将详细介绍从后端接口设计到前端 Vue.js 表格渲染的完整流程,帮助开发者掌握这一核心技能。

技术栈准备

在实现表格与数据库字段连接前,需明确整体技术栈,确保前后端协同工作:

  • 前端:Vue 3(Composition API)+ Vite(构建工具)+ Element Plus(UI 组件库,提供表格组件)
  • 后端:Node.js + Express(或 Spring Boot/Django,根据团队技术选型)
  • 数据库:MySQL/PostgreSQL(关系型数据库,支持字段映射)
  • 数据请求:Axios(HTTP 客户端,用于前后端数据交互)
  • 状态管理:Pinia(Vue 3 推荐的状态管理方案)
  • 代码规范:ESLint + Prettier(确保代码质量和一致性)

后端接口设计:连接数据库与前端

前端表格的数据来源是后端接口,因此后端需提供能查询数据库字段并返回结构化数据的 API,以 Node.js + Express 为例,实现步骤如下:

数据库表设计

假设有一个用户表 users,包含以下字段:

CREATE TABLE users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(50) NOT NULL,
  email VARCHAR(100) NOT NULL,
  age INT,
  status TINYINT DEFAULT 1 COMMENT '用户状态:1-正常,0-禁用',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

后端 API 接口开发

使用 Express + Sequelize(ORM 框架)封装查询接口,返回数据库字段对应的 JSON 数据:

// app.js
const express = require('express');
const { Sequelize, DataTypes } = require('sequelize');
const cors = require('cors');
const app = express();
// 中间件配置
app.use(cors());
app.use(express.json());
// 数据库连接(以 MySQL 为例)
const sequelize = new Sequelize('test_db', 'root', 'password', {
  host: 'localhost',
  dialect: 'mysql',
  logging: process.env.NODE_ENV === 'development' ? console.log : false
});
// 定义 User 模型(映射 users 表)
const User = sequelize.define('User', {
  username: { 
    type: DataTypes.STRING, 
    allowNull: false,
    validate: {
      notEmpty: true,
      len: [2, 50]
    }
  },
  email: { 
    type: DataTypes.STRING, 
    allowNull: false,
    unique: true,
    validate: {
      isEmail: true
    }
  },
  age: { 
    type: DataTypes.INTEGER,
    validate: {
      min: 0,
      max: 150
    }
  },
  status: {
    type: DataTypes.TINYINT,
    defaultValue: 1
  }
});
// 数据库同步
sequelize.sync({ force: false }).then(() => {
  console.log('Database synced successfully');
});
// 查询用户列表接口(GET /api/users)
app.get('/api/users', async (req, res) => {
  try {
    const { page = 1, limit = 10, keyword, status } = req.query;
    const offset = (page - 1) * limit;
    const whereClause = {};
    if (keyword) {
      whereClause[Sequelize.Op.or] = [
        { username: { [Sequelize.Op.like]: `%${keyword}%` } },
        { email: { [Sequelize.Op.like]: `%${keyword}%` } }
      ];
    }
    if (status !== undefined) {
      whereClause.status = status;
    }
    const { count, rows } = await User.findAndCountAll({
      where: whereClause,
      attributes: ['id', 'username', 'email', 'age', 'status', 'created_at'],
      order: [['id', 'DESC']],
      limit: parseInt(limit),
      offset: parseInt(offset)
    });
    res.json({
      code: 0,
      data: {
        list: rows,
        pagination: {
          total: count,
          current: parseInt(page),
          pageSize: parseInt(limit)
        }
      },
      message: 'success'
    });
  } catch (error) {
    console.error('Error fetching users:', error);
    res.status(500).json({ 
      code: 1, 
      message: 'Server error',
      error: process.env.NODE_ENV === 'development' ? error.message : undefined
    });
  }
});
// 获取单个用户详情(GET /api/users/:id)
app.get('/api/users/:id', async (req, res) => {
  try {
    const user = await User.findByPk(req.params.id, {
      attributes: ['id', 'username', 'email', 'age', 'status', 'created_at']
    });
    if (!user) {
      return res.status(404).json({ 
        code: 1, 
        message: 'User not found' 
      });
    }
    res.json({
      code: 0,
      data: user,
      message: 'success'
    });
  } catch (error) {
    res.status(500).json({ 
      code: 1, 
      message: 'Server error' 
    });
  }
});
app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

关键点

  • 接口返回的数据需包含前端表格需要的字段(如 idusernameemail 等)
  • 通过 attributes 指定返回字段,避免敏感数据泄露
  • 支持分页、排序、筛选等参数,提升用户体验
  • 添加了输入验证和错误处理,确保数据安全性
  • 使用 CORS 中间件解决跨域问题
  • 开发环境启用日志,便于调试

前端 Vue.js 实现:表格与字段绑定

创建 Vue 项目并安装依赖

使用 Vite 创建 Vue 3 项目,安装 Element Plus、Axios 和 Pinia:

npm create vue@latest vue-table-demo
cd vue-table-demo
npm install element-plus axios pinia @element-plus/icons-vue
npm install -D sass

配置 Axios 请求封装

src/utils/request.js 中封装 Axios,统一处理请求头、错误等:

import axios from 'axios';
import { ElMessage } from 'element-plus';
import router from '@/router';
const request = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});
// 请求拦截器
request.interceptors.request.use(
  config => {
    // 从 localStorage 获取 token
    const token = localStorage.getItem('token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);
// 响应拦截器
request.interceptors.response.use(
  response => {
    const res = response.data;
    // 根据业务码处理响应
    if (res.code === 0) {
      return res.data;
    } else {
      ElMessage.error(res.message || '请求失败');
      return Promise.reject(new Error(res.message || 'Error'));
    }
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // 未授权,清除 token 并跳转到登录

标签: #数据库字段