当前位置:首页 > VUE

vue实现通讯录代码

2026-01-12 05:44:38VUE

实现思路

Vue实现通讯录的核心在于数据管理和组件交互。通常需要包含联系人列表、搜索功能、分组索引以及添加/编辑功能。

vue实现通讯录代码

基本结构

创建Vue组件时需包含以下部分:

  • 数据模型:存储联系人数组
  • 字母索引栏:实现快速跳转
  • 搜索框:过滤联系人
  • 列表渲染:展示联系人信息

核心代码示例

数据模型

data() {
  return {
    contacts: [
      { name: '张三', phone: '13800138000', initial: 'Z' },
      { name: '李四', phone: '13900139000', initial: 'L' }
    ],
    searchText: ''
  }
}

计算属性处理数据

computed: {
  filteredContacts() {
    return this.contacts.filter(item => 
      item.name.includes(this.searchText) || 
      item.phone.includes(this.searchText)
    )
  },
  groupedContacts() {
    const groups = {}
    this.filteredContacts.forEach(item => {
      const initial = item.initial.toUpperCase()
      if (!groups[initial]) {
        groups[initial] = []
      }
      groups[initial].push(item)
    })
    return groups
  },
  initials() {
    return Object.keys(this.groupedContacts).sort()
  }
}

模板部分

<div class="address-book">
  <input v-model="searchText" placeholder="搜索联系人">

  <div class="index-bar">
    <span 
      v-for="initial in initials" 
      :key="initial"
      @click="scrollTo(initial)"
    >
      {{ initial }}
    </span>
  </div>

  <div class="contact-list">
    <div v-for="(group, initial) in groupedContacts" :key="initial">
      <h3 :id="initial">{{ initial }}</h3>
      <div 
        v-for="contact in group" 
        :key="contact.phone"
        class="contact-item"
      >
        <span>{{ contact.name }}</span>
        <span>{{ contact.phone }}</span>
      </div>
    </div>
  </div>
</div>

滚动定位方法

methods: {
  scrollTo(initial) {
    const element = document.getElementById(initial)
    if (element) {
      element.scrollIntoView({ behavior: 'smooth' })
    }
  }
}

样式优化建议

.address-book {
  position: relative;
  height: 100vh;
}
.index-bar {
  position: fixed;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
}
.contact-list {
  overflow-y: auto;
  height: calc(100% - 60px);
}
.contact-item {
  padding: 10px;
  border-bottom: 1px solid #eee;
}

功能扩展建议

  • 添加本地存储功能保存联系人
  • 实现联系人详情页和编辑功能
  • 添加手机震动反馈当点击字母索引时
  • 优化移动端触摸事件处理

性能优化

对于大型通讯录,建议:

  • 使用虚拟滚动技术
  • 对列表数据进行分页加载
  • 使用Web Worker处理搜索过滤

以上代码提供了Vue实现通讯录的基本框架,可根据实际需求进行调整和扩展。

vue实现通讯录代码

标签: 通讯录代码
分享给朋友:

相关文章

vue实现通讯录代码

vue实现通讯录代码

以下是一个基于Vue.js实现的通讯录示例代码,包含联系人列表、搜索和添加功能: 实现步骤 创建Vue组件 <template> <div class="address-bo…

js实现代码雨

js实现代码雨

实现代码雨效果 使用HTML5 Canvas和JavaScript可以轻松实现经典的代码雨效果。以下是完整的实现代码和说明: HTML结构 <!DOCTYPE html> <ht…

vue实现网站代码

vue实现网站代码

Vue 实现网站代码的基本步骤 安装 Vue.js 通过 npm 或 CDN 引入 Vue.js。使用 npm 安装:npm install vue。或者直接在 HTML 文件中引入 Vue CDN:…

vue实现动画代码

vue实现动画代码

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,包括内置过渡组件、CSS 动画库集成以及第三方动画库结合。以下是几种常见实现方法: 使用 Vue 内置过渡组件 Vue 的 <tran…

vue拖拽实现低代码

vue拖拽实现低代码

Vue拖拽实现低代码方案 基于现成库的方案(推荐) 使用成熟的拖拽库可以快速实现低代码功能,例如: vuedraggable:基于Sortable.js的Vue拖拽组件 vue-draggable-…

vue实现动态修改代码

vue实现动态修改代码

Vue 动态修改代码的实现方法 Vue 提供了多种方式实现动态修改代码的需求,以下是几种常见场景和解决方案: 动态组件加载 使用 Vue 的 <component> 标签配合 is 属…