vue本地缓存搜索记录(最多4条)

核心代码

      //保存到搜索历史,最多存四个 item.name和item.code格式为:塞力斯000001
      var history = uni.getStorageSync('history') || [];
      console.log("history==", history)
      var index = history.findIndex((items) => {
        return item.name == items.name && item.code == items.code;
      });
      if (index != -1) {
        history.splice(index, 1);
      }
      history.unshift({
        name: item.name,
        code: item.code,
        foxxcode: item.foxxcode,
      });
      uni.setStorageSync('history', history.slice(0, 4));

全部代码

<template>
  <view class="u-list-item" :class="'line-'+themeColor.name">

    <view @click="gotoDetail(item)" style="display: flex;flex-direction: row;width: 90%;">
      <text :class="'text-'+themeColor.name" class="list-item-name">{
  { item.name }}</text>
      <text class="list-item-sx">{
  { item.code }}</text>
      <text class="list-item-sx">{
  { item.sx }}</text>
    </view>

    <view style="flex-direction: column;float: right;align-items: center;" v-show="!this.isCollection">
      <image @click="collection(item.code)" class="icon-right-child" :src="'/static/add.png'"/>
    </view>

  </view>
</template>

<script>
export default {
  data() {
    return {
      group_id: "",
      isLogin: false,
      isCollection: false,
      codes: [],
    };
  },

  created() {
    this.group_id = this.$groudId();
    this.isLogin = this.$isLogin();
    this.codes = this.$focusCodes();
    if (this.codes.indexOf(this.item.code) > 0) {
      this.isCollection = true;
    }

  },
  props: {
    item: Object,
    iconsize: {
      type: String,
      default: '36'
    },
    fontweight: {
      type: String,
      default: 'blod'
    },
    fontsize: {
      type: String,
      default: '30'
    }
  },

  methods: {
    gotoDetail(item) {
      uni.navigateTo({
        url: "../main/stock-detail?code=" + item.code + "&foxxcode=" + item.foxxcode
      })
      //保存到搜索历史,最多存四个 item.name和item.code格式为:塞力斯000001
      var history = uni.getStorageSync('history') || [];
      console.log("history==", history)
      var index = history.findIndex((items) => {
        return item.name == items.name && item.code == items.code;
      });
      if (index != -1) {
        history.splice(index, 1);
      }
      history.unshift({
        name: item.name,
        code: item.code,
        foxxcode: item.foxxcode,
      });
      uni.setStorageSync('history', history.slice(0, 4));

    },
    collection(code) {
      if (!this.isLogin) {
        this.$toast("请先登录");
        return;
      }
      if (this.group_id != "") {
        var _this = this;
        this.request({
          urlInfo: this.$REQUEST.INSERT_STOCK,
          body: {
            group_id: this.group_id,
            codes: code,
          },
          success(res) {
            _this.isCollection = true;
            console.log("insert_stocks-success==", res)
          },
          fail: (res) => {
            console.log("insert_stocks-fail==", res)
          }
        })
      }
    },

  }
};
</script>

<style lang="scss">
.u-list-item {
  height: 100rpx;
  align-items: center;
  display: flex;
  align-items: center;
  border-bottom-style: solid;
  border-bottom-width: 0.5px;
  margin-left: 30rpx;
  margin-right: 30rpx;
}

.list-item-name {
  margin-left: 20rpx;
  font-weight: bold;
}

.list-item-sx {
  margin-left: 20rpx;
  padding-top: 6rpx;
  color: #7F7F7F;
}

.icon-right-child {
  width: 45rpx;
  height: 45rpx;
  margin-top: 10rpx;
}
</style>

点击清除历史

   clearhistory() {
      uni.removeStorageSync('history');
      this.historyList = [];
    },

使用

  onShow() {
    this.historyList = uni.getStorageSync('history') || [];
    console.log("historyList==", this.historyList)
  },

相关推荐

  1. neo4j中如何并列执行命令

    2023-12-27 21:16:04       55 阅读
  2. milvus2.4向量搜索源码分析

    2023-12-27 21:16:04       27 阅读
  3. 本地声明式缓存

    2023-12-27 21:16:04       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 21:16:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 21:16:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 21:16:04       87 阅读
  4. Python语言-面向对象

    2023-12-27 21:16:04       96 阅读

热门阅读

  1. F (1164) : B DS二叉排序树_有效的二叉排序树

    2023-12-27 21:16:04       63 阅读
  2. 编译时AndroidX冲突报错

    2023-12-27 21:16:04       48 阅读
  3. Request对象和Response对象

    2023-12-27 21:16:04       56 阅读
  4. 数据可视化期末整理(自用)

    2023-12-27 21:16:04       53 阅读
  5. 蓝桥杯python比赛历届真题经典练习题 (1-3)

    2023-12-27 21:16:04       60 阅读
  6. 悟的数组匹配

    2023-12-27 21:16:04       63 阅读
  7. 基于大数据技术的智慧城市交通流量预测与优化

    2023-12-27 21:16:04       45 阅读
  8. http笔记

    2023-12-27 21:16:04       51 阅读
  9. 三、typescript 接口定义/继承/类

    2023-12-27 21:16:04       56 阅读
  10. 软件工程面试题

    2023-12-27 21:16:04       42 阅读