当前位置:   article > 正文

vue实战之搜索框功能实现_搜索框vue

搜索框vue

一、静态页面

  1. <template>
  2. <div class="search">
  3. <div class="top">
  4. <van-nav-bar left-text="返回" left-arrow @click-left="$router.go(-1)" />
  5. <van-search autofocus="true" shape="round" v-model="search" show-action placeholder="请输入搜索关键词"
  6. @search="goSearch(search)" />
  7. </div>
  8. <div class="history">
  9. <div class="title">
  10. <span>最近搜索</span>
  11. <van-icon @click="clear" name="delete-o" size="16" />
  12. </div>
  13. <div class="list" v-if="history.length !== 0">
  14. <div v-for="item in history" :key="item" class="list-item">
  15. <div @click="goSearch(item)">
  16. {{ item }}
  17. </div>
  18. <div>
  19. <button @click="deleteHistory(item)"></button>
  20. </div>
  21. </div>
  22. </div>
  23. <div class="empty" v-else>暂无搜索记录</div>
  24. </div>
  25. <div class="result" v-if="list.length != 0">
  26. <div class="contain" v-for="(item, index) in list" v-bind:key="index" @click="getTodeatil(item.pid)">
  27. <van-image :src="item.smallImg" width="92%" height="60%" />
  28. <div class="detail">
  29. <div class="name">{{ item.name }}</div>
  30. <div class="enname">{{ item.enname }}</div>
  31. <div class="price">¥{{ item.price }}</div>
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. </template>

二、样式美化

 

  1. <style lang="less" scoped>
  2. .search {
  3. width: 100%;
  4. height: 100%;
  5. position: fixed;
  6. display: flex;
  7. flex-direction: column;
  8. align-items: center;
  9. .top {
  10. width: 100%;
  11. display: flex;
  12. justify-content: space-around;
  13. align-items: center;
  14. }
  15. .van-nav-bar {
  16. width: 24%;
  17. }
  18. ::v-deep .van-nav-bar__text {
  19. color: rgb(37, 72, 189);
  20. }
  21. ::v-deep .van-nav-bar .van-icon {
  22. color: rgb(37, 72, 189);
  23. }
  24. .van-search {
  25. width: 80%;
  26. }
  27. .history {
  28. width: 98%;
  29. background-color: aliceblue;
  30. border-radius: 10px;
  31. display: flex;
  32. flex-direction: column;
  33. align-items: center;
  34. .title {
  35. height: 40px;
  36. width: 90%;
  37. line-height: 40px;
  38. font-size: 14px;
  39. display: flex;
  40. justify-content: space-between;
  41. align-items: center;
  42. padding: 0 15px;
  43. }
  44. .list {
  45. display: flex;
  46. justify-content: flex-start;
  47. flex-wrap: wrap;
  48. width: 96%;
  49. .list-item {
  50. text-align: center;
  51. border-radius: 10px;
  52. padding: 5px;
  53. line-height: 16px;
  54. background: rgba(128, 128, 128, 0.4);
  55. font-size: 10px;
  56. border: 1px solid #efefef;
  57. overflow: hidden;
  58. white-space: nowrap;
  59. margin: 5px 5px;
  60. text-overflow: ellipsis;
  61. display: flex;
  62. align-items: center;
  63. justify-content: space-around;
  64. button {
  65. border: 0;
  66. outline: none;
  67. background-color: transparent;
  68. z-index: 99;
  69. }
  70. }
  71. }
  72. .empty {
  73. text-align: center;
  74. background-color: rgba(0, 255, 191, 0.1);
  75. border-radius: 10px;
  76. padding: 4px;
  77. }
  78. }
  79. .result {
  80. width: 100%;
  81. display: flex;
  82. flex-wrap: wrap;
  83. justify-content: flex-start;
  84. overflow: auto;
  85. align-items: center;
  86. .contain {
  87. background-color: aliceblue;
  88. width: 42%;
  89. border-radius: 10px;
  90. height: 220px;
  91. display: flex;
  92. flex-direction: column;
  93. align-items: center;
  94. margin-bottom: 20px;
  95. margin-right: calc(10% / 4);
  96. margin-left: 16px;
  97. &:nth-child(1) {
  98. margin-top: 10px;
  99. }
  100. &:nth-child(2) {
  101. margin-top: 10px;
  102. }
  103. &:nth-child(2n) {
  104. margin-right: 0;
  105. }
  106. .van-image {
  107. margin-top: 4%;
  108. }
  109. .detail {
  110. width: 92%;
  111. margin-top: 4%;
  112. height: 30%;
  113. display: flex;
  114. flex-direction: column;
  115. align-items: flex-start;
  116. justify-content: space-around;
  117. .name {
  118. font-size: 16px;
  119. font-weight: 800;
  120. }
  121. .enname {
  122. color: rgba(128, 128, 128, 0.5);
  123. font-size: 12px;
  124. }
  125. .price {
  126. color: red;
  127. font-weight: 700;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. </style>

三、灵魂逻辑

  1. <script setup>
  2. import axios from 'axios'
  3. import { ref, onMounted } from 'vue'
  4. import { useRouter } from 'vue-router'
  5. const router = useRouter()
  6. const search = ref('')
  7. const history = ref([])
  8. const list = ref([])
  9. import { Toast } from 'vant';
  10. const goSearch = (e) => {
  11. console.log(search.value)
  12. search.value = e
  13. if (search.value == '') {
  14. Toast('请输入关键字!');
  15. } else {
  16. axios
  17. .get('请求接口', {
  18. params: {
  19. 参数值1:值,
  20. name: search.value//商品关键字
  21. }
  22. })
  23. .then(function (response) {
  24. console.log('search获取成功', response.data)
  25. if (response.data.result.length != 0) {
  26. list.value = response.data.result
  27. addHistory()
  28. saveSearchHistory()
  29. } else {
  30. addHistory()
  31. saveSearchHistory()
  32. Toast('暂无商品!');
  33. search.value = ''
  34. }
  35. })
  36. .catch(function (error) {
  37. console.log(error)
  38. })
  39. }
  40. }
  41. const getSearchHistory = () => {
  42. const historyData = localStorage.getItem('history')
  43. if (historyData) {
  44. history.value = JSON.parse(historyData)
  45. }
  46. }
  47. const addHistory = () => {
  48. if (!history.value.includes(search.value)) {
  49. history.value.unshift(search.value)
  50. if (history.value.length > 10) {
  51. history.value.pop()
  52. }
  53. }
  54. }
  55. //点击商品进行跳转到该商品的详情页
  56. const getTodeatil = (pid) => {
  57. router.push({
  58. path: '/detail',
  59. query: {
  60. pid
  61. }
  62. })
  63. }
  64. onMounted(() => {
  65. getSearchHistory()
  66. })
  67. const saveSearchHistory = () => {
  68. localStorage.setItem('history', JSON.stringify(history.value))
  69. }
  70. const clear = () => {
  71. if (history.value.length != 0) {
  72. history.value = []
  73. saveSearchHistory()
  74. } else {
  75. Toast('暂无搜索记录!');
  76. }
  77. }
  78. const deleteHistory = (item) => {
  79. const index = history.value.indexOf(item)
  80. if (index !== -1) {
  81. history.value.splice(index, 1)
  82. saveSearchHistory()
  83. }
  84. }
  85. </script>

最终效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/636514
推荐阅读
相关标签
  

闽ICP备14008679号