当前位置:首页 > VUE

vue实现搜索文章标题

2026-02-24 15:41:38VUE

实现搜索文章标题功能

在Vue中实现搜索文章标题功能,可以通过以下方式完成:

数据准备 假设有一个文章列表数据,存储在一个数组中:

data() {
  return {
    articles: [
      { id: 1, title: 'Vue入门教程' },
      { id: 2, title: 'React高级技巧' },
      { id: 3, title: 'JavaScript设计模式' }
    ],
    searchQuery: ''
  }
}

模板部分 创建输入框和搜索结果展示区域:

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索文章标题">
    <ul>
      <li v-for="article in filteredArticles" :key="article.id">
        {{ article.title }}
      </li>
    </ul>
  </div>
</template>

计算属性实现搜索 使用计算属性实现实时搜索功能:

computed: {
  filteredArticles() {
    const query = this.searchQuery.toLowerCase()
    return this.articles.filter(article => 
      article.title.toLowerCase().includes(query)
    )
  }
}

优化搜索功能

添加防抖处理 防止频繁触发搜索,优化性能:

vue实现搜索文章标题

data() {
  return {
    debounceTimeout: null
  }
},
methods: {
  debounceSearch() {
    clearTimeout(this.debounceTimeout)
    this.debounceTimeout = setTimeout(() => {
      this.filteredArticles = this.performSearch()
    }, 300)
  },
  performSearch() {
    const query = this.searchQuery.toLowerCase()
    return this.articles.filter(article => 
      article.title.toLowerCase().includes(query)
    )
  }
}

高亮匹配文本 在搜索结果中高亮显示匹配的文本:

methods: {
  highlight(text) {
    const query = this.searchQuery
    if (!query) return text
    const regex = new RegExp(query, 'gi')
    return text.replace(regex, match => `<span class="highlight">${match}</span>`)
  }
}

在模板中使用v-html绑定高亮文本:

<li v-for="article in filteredArticles" :key="article.id">
  <span v-html="highlight(article.title)"></span>
</li>

服务器端搜索实现

对于大量数据,建议使用服务器端搜索:

vue实现搜索文章标题

API调用示例

methods: {
  async searchArticles() {
    try {
      const response = await axios.get('/api/articles', {
        params: {
          search: this.searchQuery
        }
      })
      this.filteredArticles = response.data
    } catch (error) {
      console.error('搜索失败:', error)
    }
  }
}

监听搜索词变化

watch: {
  searchQuery(newVal) {
    if (newVal.length > 2 || newVal.length === 0) {
      this.debounceSearch()
    }
  }
}

用户体验优化

添加加载状态

data() {
  return {
    isLoading: false
  }
},
methods: {
  async searchArticles() {
    this.isLoading = true
    try {
      // API调用
    } finally {
      this.isLoading = false
    }
  }
}

模板中添加加载指示器

<div v-if="isLoading">搜索中...</div>
<div v-else-if="filteredArticles.length === 0 && searchQuery">
  没有找到匹配的文章
</div>

这些方法可以根据实际项目需求进行组合和调整,实现适合的搜索功能。

标签: 标题文章
分享给朋友:

相关文章

react文章实现

react文章实现

React 文章实现方法 创建 React 项目 使用 create-react-app 或 Vite 初始化项目。例如: npx create-react-app article-app 或 n…

react实现博客文章概要

react实现博客文章概要

实现博客文章概要的几种方法 使用字符串截取 通过截取文章内容的前N个字符作为概要,适用于纯文本内容。注意处理中文字符和截断位置。 function getSummary(content, lengt…

css标题制作

css标题制作

使用CSS制作标题的方法 基础样式设置 通过font-size、font-weight和color属性快速定义标题外观: h1 { font-size: 2.5rem; font-weigh…

vue实现文章锚点定位

vue实现文章锚点定位

实现锚点定位的基本方法 使用Vue实现文章锚点定位可以通过以下几种方式实现: 使用HTML原生锚点 在Vue模板中直接使用HTML的id属性和a标签的href属性实现跳转。这种方法简单直接,但页面会…

vue实现文章滚动

vue实现文章滚动

实现文章滚动的基本思路 在Vue中实现文章滚动效果,可以通过多种方式实现,包括使用CSS动画、JavaScript滚动API或第三方库。以下是几种常见的实现方法: 使用CSS动画实现平滑滚动 通过C…

php实现文章搜索

php实现文章搜索

PHP实现文章搜索的方法 使用MySQL的LIKE语句进行基础搜索 在PHP中可以通过MySQL的LIKE语句实现简单的文章搜索功能。这种方法适合小规模数据量的搜索需求。 $keyword = $…