首页 > 其他分享 >Vue项目搜索-历史记录管理

Vue项目搜索-历史记录管理

时间:2023-11-26 11:45:58浏览次数:25  
标签:历史记录 Vue const addHistory 搜索 key history

搜索-历史记录管理

目标:构建搜索页的静态布局,完成历史记录的管理

需求:

  • 搜索历史基本渲染
  • 点击搜索(添加历史)
    • 点击搜索按钮 或底下历史记录,都能进行搜索
    • 若之前没有相同搜索关键字,则直接追加到最前面
    • 若之前已有相同搜索关键字,将该原有关键字移除,在追加(相当于置顶)
  • 清空历史:添加清空图标,可以清空历史记录
  • 持久化:搜索历史需要持久化,刷新历史不丢失

定义storage 本地js文件

// 商品的key
const HISTORY_KEY = 'hm_shopping_history'
// 添加历史记录
export const addHistory = (goods) => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(goods))
}

// 获取历史记录
export const getHistory = () => {
  const defalut = []
  const result = localStorage.getItem(HISTORY_KEY)
  return result ? JSON.parse(result) : defalut
}
// 删除 历史记录
export const removeHistory = () => {
  localStorage.removeItem(HISTORY_KEY)
}

定义vuex模块

import { getHistory, addHistory } from '@/utils/storage'
const state = {
  // 个人权证相关
  // 从本地获取用户信息
  history: getHistory()
}
const mutations = {
  // 提供一个存储 history 的方法
  setHistory (state, obj) {
    state.history = obj
    // 将商品信息存储到本地
    addHistory(obj)
  }
}
const actions = {

}
const getters = {

}

// 对外暴露
export default {
  namespaced: true, // 开启命名空间,用于mapState 映射
  state,
  mutations,
  actions,
  getters
}

页面组件使用

<script>
import { getHistory, addHistory } from '@/utils/storage'
export default {
  name: 'SearchIndex',
  data () {
    return {
      search: '', // 输入框内容
      history: getHistory() // 历史记录
    }
  },
  methods: {
    // 搜索
    goSearch (key) {
      if (key === '') {
        this.$toast('请输入搜索内容')
        return
      }
      // console.log('点击了搜索:' + key)
      const index = this.history.indexOf(key)
      if (index !== -1) {
        // 说明数组中存在相同的历史记录,此时删除原有的关键字
        this.history.splice(index, 1)
      }
      // unshift 追加到数组第一个
      this.history.unshift(key)
      // 调用 localStorage.setItem(HISTORY_KEY, JSON.stringify(goods)),保存到本地
      addHistory(this.history)
      // 跳转到搜索列表页
      this.$router.push('/searchlist?search=' + key)
    },
    // 清空
    clear () {
      this.history = []
      addHistory(this.history)
    }
  }

}
</script>

标签:历史记录,Vue,const,addHistory,搜索,key,history
From: https://www.cnblogs.com/zgf123/p/17856669.html

相关文章

  • 【前端VUE】Vue3路由设置(Typescript版本)
    新建项目npmcreatevite@latest安装vue-routercd.\my-web\npminstallvue-router在src->components下新增(Home.vue)<template><h1>我是主页</h1></template>在src->components下新增(Register.vue)<template><h1>用户......
  • Vue项目的创建、运行与端口号修改
    前言:Vue-cli是Vue官方提供的一个脚手架,用于快速生成一个Vue的项目模板,依赖于NodeJS环境NodeJS下载:NodeJS安装下载Vue-cli下载:Vue-cli下载一.Vue图形化创建项目1.建立一个文件夹,保存Vue项目2.在该文件夹的目录上输入cmd打开命令行3.令行输入vueui打开Vue项目管理......
  • 搜索功能
    07-01搜索导航栏一.流程'''#拓展:京东360buy->jd#全文检索问题:数据量的庞大解决:全文检索引擎(elasticsearch一来七课社区).java封装的一个数据库,专注于大数据的搜索#前端:头部搜索组件+搜索页面1.新建页面:SearchCours......
  • 【前端VUE】Vue3条件渲染指令(v-if、v-else、v-else-if、v-show、v-for)
    新建项目npmcreatevite@latest运行项目cd项目目录npminstallnpmrundev条件渲染指令1、v-ifv-if指令用于条件性地渲染一块内容。这块内容只会在指令的表达式返回真值时才被渲染。2、v-else可以使用v-else为v-if添加一个“else区块”。3、v-else-ifv-else......
  • vue 根据js的变量来设置css 里面的属性的属性值
    1.通过动态绑定style,声明css变量"--fontColor",把变量”fontColor”赋给“--fontColor”2.在css中使用var函数读取“--fontColor”变量点击查看代码<template><divclass="wen_style":style="{'--fontColor':fontColor}">当前字体的颜色......
  • Vue3 - Teleport 传送门
    前言对比Vue2,引出并展开Vue3。本文讲述了Teleport传送门是什么,以及使用方法和代码示例。介绍学过React的同学可能知道,Portal也提供了一种将子节点渲染到存在于父组件以外的DOM节点的优秀方案,当然咱们大名鼎鼎的Vue3也做到了,关于这方面的功能需求,第三方库也是有......
  • vue脚手架的练习1
    <template><divclass="project"><first-page></first-page><second-page></second-page></div></template><script>importFirstPagefrom'./FirstPage1.vue'importSecon......
  • Java基于协同过滤算法开发的springboot+vue服装商城
    演示视频https://www.bilibili.com/video/BV1oH4y127fq/?share_source=copy_web&vd_source=11344bb73ef9b33550b8202d07ae139b主要功能:用户可以浏览商品和特价商品,加入购物车,直接下单支付,在我的个人中心里可以管理自己的订单,收货地址,编辑资料等。管理员可以发布商品,上下架商品,处......
  • 机器学习——K近邻算法-kd(简化因数据过过多而造成的搜索复杂度大)
    kd树是为了减少搜索最近邻点的时间复杂度,一般来说可以使用穷举法,但是太耗时,因此采用平衡二叉树的思想来解决这个问题"""ThisistheimplementationofKnn(KdTree),whichisaccessibleinhttps://github.com/FlameCharmander/MachineLearning,accomplishedbyFlameCharma......
  • vue font awasome 的使用
    fontawasome是一个很经典的图标库。目前已经更新到了V6版本。但是从V5开始,这个库就已经闭源收费了。所以开源库里面最新版本是V4.7。https://www.npmjs.com/package/font-awesome/v/4.7.0 那么怎么使用呢?main.jsimportVuefrom'vue'importAppfrom'./App.vue'i......