当前位置:首页 > VUE

vue实现input输入提示

2026-02-23 04:23:05VUE

Vue 实现 Input 输入提示的方法

使用 v-model 和计算属性

通过 v-model 绑定输入框的值,结合计算属性或方法实现输入提示功能。计算属性可以监听输入变化并返回匹配的提示列表。

<template>
  <div>
    <input v-model="inputValue" @input="handleInput" />
    <ul v-if="suggestions.length">
      <li v-for="(item, index) in suggestions" :key="index" @click="selectSuggestion(item)">
        {{ item }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputValue: '',
      allItems: ['Apple', 'Banana', 'Orange', 'Mango'],
      suggestions: []
    }
  },
  methods: {
    handleInput() {
      this.suggestions = this.allItems.filter(item =>
        item.toLowerCase().includes(this.inputValue.toLowerCase())
      )
    },
    selectSuggestion(item) {
      this.inputValue = item
      this.suggestions = []
    }
  }
}
</script>

使用第三方库 vue-autosuggest

vue-autosuggest 是一个专门为 Vue 设计的自动完成组件,提供更丰富的功能和配置选项。

安装依赖:

npm install vue-autosuggest

使用示例:

vue实现input输入提示

<template>
  <vue-autosuggest
    :suggestions="filteredOptions"
    :input-props="{ placeholder: 'Search...' }"
    @input="onInputChange"
    @selected="onSelected"
  >
    <template #suggestion="{ suggestion }">
      {{ suggestion.item }}
    </template>
  </vue-autosuggest>
</template>

<script>
import { VueAutosuggest } from 'vue-autosuggest'

export default {
  components: {
    VueAutosuggest
  },
  data() {
    return {
      suggestions: [],
      allOptions: ['Red', 'Green', 'Blue', 'Yellow']
    }
  },
  computed: {
    filteredOptions() {
      return [
        {
          data: this.suggestions
        }
      ]
    }
  },
  methods: {
    onInputChange(text) {
      this.suggestions = this.allOptions.filter(option =>
        option.toLowerCase().includes(text.toLowerCase())
      )
    },
    onSelected(item) {
      console.log('Selected:', item)
    }
  }
}
</script>

使用 Element UI 的 Autocomplete 组件

如果项目中使用 Element UI,可以直接使用其提供的 Autocomplete 组件实现输入提示功能。

安装 Element UI:

vue实现input输入提示

npm install element-ui

使用示例:

<template>
  <el-autocomplete
    v-model="inputValue"
    :fetch-suggestions="querySearch"
    placeholder="请输入内容"
    @select="handleSelect"
  />
</template>

<script>
export default {
  data() {
    return {
      inputValue: '',
      items: ['JavaScript', 'TypeScript', 'Vue', 'React']
    }
  },
  methods: {
    querySearch(queryString, cb) {
      const results = queryString
        ? this.items.filter(item =>
            item.toLowerCase().includes(queryString.toLowerCase())
          )
        : this.items
      cb(results)
    },
    handleSelect(item) {
      console.log(item)
    }
  }
}
</script>

自定义指令实现输入提示

对于需要更高度定制化的场景,可以创建自定义指令来实现输入提示功能。

<template>
  <input v-model="inputValue" v-input-suggest="suggestions" />
</template>

<script>
export default {
  directives: {
    inputSuggest: {
      bind(el, binding, vnode) {
        el.addEventListener('input', () => {
          const value = el.value
          const suggestions = binding.value
          // 实现提示逻辑
        })
      }
    }
  },
  data() {
    return {
      inputValue: '',
      suggestions: ['Option 1', 'Option 2', 'Option 3']
    }
  }
}
</script>

使用 Vue 3 Composition API

在 Vue 3 中,可以使用 Composition API 更灵活地实现输入提示功能。

<template>
  <input v-model="inputValue" @input="updateSuggestions" />
  <ul v-if="suggestions.length">
    <li v-for="(item, index) in suggestions" :key="index" @click="selectItem(item)">
      {{ item }}
    </li>
  </ul>
</template>

<script>
import { ref, computed } from 'vue'

export default {
  setup() {
    const inputValue = ref('')
    const allItems = ['Vue', 'React', 'Angular', 'Svelte']
    const suggestions = ref([])

    const updateSuggestions = () => {
      suggestions.value = allItems.filter(item =>
        item.toLowerCase().includes(inputValue.value.toLowerCase())
      )
    }

    const selectItem = item => {
      inputValue.value = item
      suggestions.value = []
    }

    return {
      inputValue,
      suggestions,
      updateSuggestions,
      selectItem
    }
  }
}
</script>

以上方法提供了不同复杂度和定制化程度的实现方案,可以根据项目需求选择最适合的方式。

标签: 提示vue
分享给朋友:

相关文章

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="app"…

vue vnode实现

vue vnode实现

Vue VNode 实现原理 VNode(Virtual Node)是 Vue 的核心概念之一,用于描述真实 DOM 的轻量级虚拟表示。VNode 的实现涉及以下关键点: VNode 的基本结构 V…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…