当前位置:首页 > VUE

vue实现静态查询

2026-02-25 16:21:05VUE

Vue 实现静态查询的方法

在 Vue 中实现静态查询通常指在不依赖后端接口的情况下,直接对本地数据进行筛选或过滤。以下是几种常见的方法:

使用计算属性过滤数据

计算属性适合基于响应式数据动态过滤结果,无需手动触发。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索..." />
    <ul>
      <li v-for="item in filteredItems" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ]
    };
  },
  computed: {
    filteredItems() {
      return this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    }
  }
};
</script>

使用方法触发查询

如果需要手动触发查询(如点击按钮),可以使用方法而非计算属性。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索..." />
    <button @click="search">查询</button>
    <ul>
      <li v-for="item in result" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      items: [
        { id: 1, name: '苹果' },
        { id: 2, name: '香蕉' },
        { id: 3, name: '橙子' }
      ],
      result: []
    };
  },
  methods: {
    search() {
      this.result = this.items.filter(item =>
        item.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    }
  }
};
</script>

使用 Vue 3 的 refcomputed

在 Vue 3 中,可以通过 refcomputed 实现类似功能。

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索..." />
    <ul>
      <li v-for="item in filteredItems" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>

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

export default {
  setup() {
    const searchQuery = ref('');
    const items = ref([
      { id: 1, name: '苹果' },
      { id: 2, name: '香蕉' },
      { id: 3, name: '橙子' }
    ]);

    const filteredItems = computed(() =>
      items.value.filter(item =>
        item.name.toLowerCase().includes(searchQuery.value.toLowerCase())
      )
    );

    return { searchQuery, filteredItems };
  }
};
</script>

结合 Lodash 进行复杂查询

若需更复杂的查询逻辑(如模糊搜索、多条件筛选),可以引入 Lodash 库。

import _ from 'lodash';

// 在计算属性或方法中使用
filteredItems() {
  return _.filter(this.items, item => 
    _.includes(item.name.toLowerCase(), this.searchQuery.toLowerCase())
  );
}

静态查询的注意事项

  • 性能优化:对于大型数据集,避免频繁触发过滤操作,可使用防抖(debounce)或节流(throttle)。
  • 大小写处理:统一转为小写或大写后再比较,避免大小写敏感问题。
  • 多字段查询:扩展过滤逻辑以支持多个字段的联合查询。

通过上述方法,可以灵活实现 Vue 中的静态查询功能。

vue实现静态查询

标签: 静态vue
分享给朋友:

相关文章

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现公告

vue实现公告

Vue 实现公告功能的方法 公告功能通常需要实现滚动展示、自动切换或固定显示的效果。以下是几种常见的实现方式: 使用 marquee 标签实现滚动公告 <template> <…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一…

vue实现数组

vue实现数组

Vue 实现数组操作的方法 在 Vue 中,数组的操作需要特别注意响应式的问题。Vue 提供了一些方法来确保数组的变动能够触发视图更新。 响应式数组方法 Vue 对以下数组方法进行了封装,确保它们能…

vue实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…