欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

商城实战项目

程序员文章站 2022-03-13 13:54:04
...

搜索功能

  • router/index.js
  1. ...
  2. const SoList=()=>import('../views/so/SoList')
  3. ...
  4. {
  5. path: '/so',
  6. name: 'SoList',
  7. component: SoList,
  8. meta: {
  9. title: '魔盒商城-搜索'
  10. }
  11. }
  • network/so.js
  1. import {request} from './request';
  2. export function getSo() {
  3. return request({url: '/api/goods',})
  4. }
  5. export function getSoGoods(title='',order = 'sales', page = 1) {
  6. return request({url: '/api/goods?title='+ title + '&page=' + page + '&' + order + '=1',})
  7. }
  • src/view/so/SoList.vue
  1. <template>
  2. <div id="soso">
  3. <div>
  4. <form action="/">
  5. <van-search
  6. v-model="value"
  7. show-action
  8. placeholder="请输入搜索关键词"
  9. @search="onSearch"
  10. @cancel="onCancel"
  11. />
  12. </form>
  13. <div class="ordertab">
  14. <van-tabs v-model="active" @click="tabClick">
  15. <van-tab title="销量排序"></van-tab>
  16. <van-tab title="价格排序"></van-tab>
  17. <van-tab title="评化排序"></van-tab>
  18. </van-tabs>
  19. </div>
  20. <div class="wrapper">
  21. <div class="content">
  22. <good-list :goods="showGoods"></good-list>
  23. </div>
  24. </div>
  25. <back-top @bTop="bTop" v-show="isTabFixed"></back-top>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. import {ref, reactive, toRef, onMounted,computed,watchEffect,nextTick} from "vue";
  31. import {getSo,getSoGoods} from "network/so";
  32. import TabControl from "components/content/tabcontrol/TabControl";
  33. import GoodList from "components/content/goods/GoodList";
  34. import BScroll from 'better-scroll';
  35. import BackTop from "components/common/backtop/BackTop";
  36. import { Search,Toast } from 'vant';
  37. export default {
  38. name: "SoList",
  39. components:{
  40. TabControl,
  41. GoodList,
  42. BackTop,
  43. BScroll
  44. },setup(){
  45. const value = ref('');
  46. let keyword=ref('');
  47. const onSearch = (val) => {
  48. Toast(val)
  49. return val;
  50. };
  51. console.log( value);
  52. const onCancel = () => Toast('取消');
  53. let isTabFixed= ref(false)
  54. let currentType=ref('sales')
  55. const goods= reactive({
  56. sales:{page:1,list:[]},
  57. new:{page:1,list:[]},
  58. recommend:{page:1,list:[]},
  59. })
  60. const showGoods=computed(()=>{
  61. return goods[currentType.value].list;
  62. })
  63. let bscroll=null;
  64. onMounted(() => {
  65. getSoGoods(value.value,'sales').then((res=>{
  66. goods.sales.list=res.data.goods.data;
  67. }))
  68. getSoGoods(value.value,'new').then((res=>{
  69. goods.new.list=res.data.goods.data;
  70. }))
  71. getSoGoods(value.value,'recommend').then((res=>{
  72. goods.recommend.list=res.data.goods.data;
  73. }))
  74. bscroll= new BScroll(document.querySelector('.wrapper'),{
  75. probeType: 3,
  76. click:true,
  77. pullUpLoad: true
  78. });
  79. bscroll.on('pullingUp',()=>{
  80. const page=goods[currentType.value].page+1;
  81. getSoGoods(value.value,currentType.value,page).then(res=>{
  82. goods[currentType.value].list.push(...res.data.goods.data)
  83. goods[currentType.value].page +=1
  84. })
  85. bscroll.finishPullUp();
  86. bscroll.refresh();
  87. console.log(page)
  88. })
  89. })
  90. const tabClick=(index)=>{
  91. let type=['sales','new','recommend'];
  92. currentType.value=type[index];
  93. nextTick(()=>{
  94. bscroll && bscroll.refresh();
  95. })
  96. }
  97. const bTop=()=>{
  98. bscroll.scrollTo(0,0,500)
  99. }
  100. watchEffect(()=>{
  101. // console.log(value.value);
  102. nextTick(()=>{
  103. bscroll && bscroll.refresh();
  104. })
  105. })
  106. return {
  107. currentType,
  108. tabClick,
  109. goods,
  110. showGoods,
  111. isTabFixed,
  112. bTop,
  113. value,
  114. onSearch,
  115. onCancel,
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped lang="scss">
  121. #soso{
  122. height: 100Vh;
  123. position: relative;
  124. }
  125. .ordertab{
  126. height: 44px;
  127. position: fixed;
  128. top:44px;
  129. left: 0;
  130. right: 0;
  131. z-index: 99;
  132. }
  133. .wrapper{
  134. position: absolute;
  135. top:88px;
  136. bottom: 50px;
  137. left: 0;
  138. right: 0;
  139. overflow: hidden;
  140. .content{
  141. }
  142. }
  143. </style>