当前位置:首页 > VUE

vue的艾特功能实现

2026-03-06 18:41:34VUE

Vue 中实现 @ 提及功能

在 Vue 中实现 @ 提及功能通常需要结合文本输入框、事件监听和用户列表展示。以下是实现步骤:

1. 监听输入框内容变化

使用 v-model 绑定输入框内容,并监听输入事件。当用户输入 @ 时,触发用户列表显示。

<template>
  <div>
    <textarea v-model="content" @input="handleInput"></textarea>
    <div v-if="showUserList">
      <ul>
        <li v-for="user in filteredUsers" @click="selectUser(user)">
          {{ user.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

2. 检测 @ 符号并过滤用户列表

vue的艾特功能实现

handleInput 方法中检测 @ 符号,并根据输入内容过滤用户列表。

<script>
export default {
  data() {
    return {
      content: '',
      showUserList: false,
      users: [
        { id: 1, name: '张三' },
        { id: 2, name: '李四' },
      ],
      filteredUsers: [],
    };
  },
  methods: {
    handleInput() {
      const cursorPos = this.$refs.textarea.selectionStart;
      const textBeforeCursor = this.content.substring(0, cursorPos);
      const atIndex = textBeforeCursor.lastIndexOf('@');

      if (atIndex !== -1) {
        const query = textBeforeCursor.substring(atIndex + 1);
        this.filteredUsers = this.users.filter(user =>
          user.name.includes(query)
        );
        this.showUserList = this.filteredUsers.length > 0;
      } else {
        this.showUserList = false;
      }
    },
    selectUser(user) {
      const cursorPos = this.$refs.textarea.selectionStart;
      const textBeforeCursor = this.content.substring(0, cursorPos);
      const atIndex = textBeforeCursor.lastIndexOf('@');

      this.content =
        this.content.substring(0, atIndex) +
        `@${user.name}` +
        this.content.substring(cursorPos);

      this.showUserList = false;
    },
  },
};
</script>

3. 添加样式和优化体验

vue的艾特功能实现

为下拉列表添加样式,并在点击外部时关闭列表。

<style>
ul {
  list-style: none;
  padding: 0;
  margin: 0;
  border: 1px solid #ccc;
  max-height: 150px;
  overflow-y: auto;
}
li {
  padding: 5px 10px;
  cursor: pointer;
}
li:hover {
  background-color: #f0f0f0;
}
</style>

4. 处理多行文本和复杂场景

如果需要支持多行文本或更复杂的场景,可以使用第三方库如 tributejsmention-vue。以下是使用 mention-vue 的示例:

import Vue from 'vue';
import Mentionable from 'mention-vue';
import 'mention-vue/dist/mention-vue.css';

Vue.use(Mentionable);
<template>
  <mentionable
    :items="users"
    :insert-space="false"
    @open="onOpen"
    @search="onSearch"
    @close="onClose"
  >
    <textarea v-model="content"></textarea>
  </mentionable>
</template>

通过以上方法,可以在 Vue 中实现基本的 @ 提及功能。根据项目需求,可以选择原生实现或使用现有库简化开发。

标签: 功能vue
分享给朋友:

相关文章

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现分类

vue实现分类

Vue实现分类功能的方法 使用v-for和计算属性 通过计算属性对数据进行分类处理,结合v-for渲染分类后的列表。示例代码: <template> <div>…

vue实现fullpage

vue实现fullpage

Vue 实现 FullPage 效果 在 Vue 中实现全屏滚动(FullPage)效果可以通过多种方式完成,以下是几种常见的方法: 使用现有库 vue-fullpage.js 安装 vue-ful…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现评价

vue实现评价

Vue实现评价功能 数据绑定与双向绑定 Vue的核心特性是数据绑定和双向绑定,通过v-model指令可以轻松实现表单输入与应用状态之间的双向绑定。在评价功能中,可以使用v-model绑定评论文本和评分…