当前位置:首页 > VUE

vue组件实现员工列表

2026-02-21 13:00:13VUE

实现员工列表的Vue组件

组件结构设计

创建一个名为EmployeeList.vue的单文件组件,包含模板、脚本和样式三部分。模板部分使用<table><ul>标签展示列表数据,脚本部分处理数据逻辑。

<template>
  <div class="employee-list">
    <table>
      <thead>
        <tr>
          <th>ID</th>
          <th>姓名</th>
          <th>部门</th>
          <th>职位</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="employee in employees" :key="employee.id">
          <td>{{ employee.id }}</td>
          <td>{{ employee.name }}</td>
          <td>{{ employee.department }}</td>
          <td>{{ employee.position }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

数据定义与获取

<script>部分定义员工数据,可通过API获取或本地静态数据。使用data()函数返回响应式数据对象。

vue组件实现员工列表

<script>
export default {
  data() {
    return {
      employees: [
        { id: 1, name: '张三', department: '技术部', position: '前端工程师' },
        { id: 2, name: '李四', department: '产品部', position: '产品经理' }
      ]
    }
  },
  methods: {
    async fetchEmployees() {
      try {
        const response = await axios.get('/api/employees');
        this.employees = response.data;
      } catch (error) {
        console.error('获取员工列表失败:', error);
      }
    }
  },
  mounted() {
    this.fetchEmployees();
  }
}
</script>

样式优化

添加CSS样式美化表格,使用Scoped样式确保样式只作用于当前组件。

<style scoped>
.employee-list {
  margin: 20px;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
th {
  background-color: #f2f2f2;
}
tr:nth-child(even) {
  background-color: #f9f9f9;
}
</style>

组件交互功能

增加排序、筛选或分页功能提升用户体验。使用计算属性实现客户端排序或筛选。

vue组件实现员工列表

<script>
export default {
  data() {
    return {
      employees: [],
      sortField: 'id',
      sortDirection: 'asc'
    }
  },
  computed: {
    sortedEmployees() {
      return [...this.employees].sort((a, b) => {
        let modifier = this.sortDirection === 'asc' ? 1 : -1;
        return a[this.sortField] > b[this.sortField] ? modifier : -modifier;
      });
    }
  },
  methods: {
    sortBy(field) {
      if (this.sortField === field) {
        this.sortDirection = this.sortDirection === 'asc' ? 'desc' : 'asc';
      } else {
        this.sortField = field;
        this.sortDirection = 'asc';
      }
    }
  }
}
</script>

组件复用与Props

通过props接收外部传入的员工数据,使组件更通用。

<script>
export default {
  props: {
    initialEmployees: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      employees: this.initialEmployees
    }
  }
}
</script>

使用示例

在父组件中引入并使用EmployeeList组件,可传入初始数据或依赖组件内部获取数据。

<template>
  <div>
    <employee-list :initial-employees="preloadedEmployees" />
  </div>
</template>

<script>
import EmployeeList from './EmployeeList.vue';

export default {
  components: { EmployeeList },
  data() {
    return {
      preloadedEmployees: [
        { id: 3, name: '王五', department: '市场部', position: '市场专员' }
      ]
    }
  }
}
</script>

标签: 组件员工
分享给朋友:

相关文章

vue实现广告组件

vue实现广告组件

Vue 实现广告组件的核心方法 数据驱动的广告内容渲染 通过 props 接收广告数据(如图片URL、跳转链接等),使用 v-bind 动态绑定属性。典型结构包含 <a> 标签嵌套 <…

vue实现组件跟随

vue实现组件跟随

Vue 实现组件跟随效果 实现组件跟随效果通常需要监听鼠标或触摸事件,动态更新组件位置。以下是几种常见实现方式: 使用鼠标事件绑定 <template> <div class=…

vue轮播组件实现

vue轮播组件实现

vue轮播组件实现方法 使用第三方库(如Swiper) 安装Swiper库: npm install swiper 在Vue组件中引入: <template> <div cl…

vue radio组件实现

vue radio组件实现

Vue Radio 组件实现 在 Vue 中实现 Radio 组件可以通过原生 HTML 的 <input type="radio"> 结合 Vue 的响应式特性来完成。以下是几种常见的实…

如何判断react组件

如何判断react组件

判断 React 组件的依据 React 组件的判断通常基于其定义方式、功能特性和代码结构。以下是几种常见的判断方法: 函数组件与类组件 函数组件是通过 JavaScript 函数定义的,接收 pr…

vue实现秒表组件

vue实现秒表组件

实现秒表组件的基本思路 使用Vue实现秒表组件需要管理时间状态、控制计时器的启动/暂停/重置功能,并通过计算属性动态显示格式化时间。核心逻辑包括利用setInterval更新计时数据,并通过生命周期钩…