similarProduct.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <script lang='ts' setup>
  2. import { Swiper, SwiperSlide } from 'swiper/vue'
  3. import { Navigation, Pagination } from 'swiper/modules'
  4. import { getGoodsRecommendListApi } from '~/api/model/goods'
  5. // import "swiper/css/pagination"
  6. import 'swiper/css'
  7. import 'swiper/css/navigation'
  8. const props = defineProps({
  9. id: { type: String, default: '' },
  10. })
  11. const swiperVertical = ref<any>(null)
  12. const modules = [Navigation, Pagination]
  13. const similarGoodsList = ref<any>([])
  14. watch(() => props.id, (id: any) => {
  15. if (!id)
  16. return
  17. getSimilarGoodsList(id)
  18. }, {
  19. immediate: true,
  20. })
  21. async function getSimilarGoodsList(id: any) {
  22. try {
  23. const params = {
  24. id,
  25. pageNo: 1,
  26. pageSize: 20,
  27. }
  28. const data: any = await getGoodsRecommendListApi(params)
  29. similarGoodsList.value = data.records
  30. }
  31. catch (error) {
  32. console.log('error', error)
  33. }
  34. }
  35. function onVerticalSwiper(swiper: any) {
  36. swiperVertical.value = swiper
  37. }
  38. function onClickLeft() {
  39. swiperVertical.value.slidePrev()
  40. }
  41. function onClickRight() {
  42. swiperVertical.value.slideNext()
  43. }
  44. </script>
  45. <template>
  46. <div v-if="similarGoodsList.length" class="w-1400px mx-auto my-160px">
  47. <h2 class="!mb-60px fw-700 text-40px text-#363C40 text-center custom-title-font">
  48. Shop Similar Products
  49. </h2>
  50. <div>
  51. <div class="w-1300px mx-auto pos-relative">
  52. <div
  53. class="pos-absolute cursor-pointer left--46px top-200px w-28px h-28px transform-translate-y--50% cursor-not-allowed !cursor-pointer flex justify-center items-center"
  54. @click="onClickLeft()"
  55. >
  56. <img src="~/assets/images/arrow_left.png" alt="" class="w-26px h-26px" srcset="">
  57. </div>
  58. <div
  59. class="pos-absolute cursor-pointer right--46px top-200px w-28px h-28px transform-translate-y--50% cursor-not-allowed !cursor-pointer flex justify-center items-center"
  60. @click="onClickRight()"
  61. >
  62. <img src="~/assets/images/arrow_right.png" alt="" class="w-26px h-26px" srcset="">
  63. </div>
  64. <Swiper
  65. v-if="similarGoodsList.length"
  66. :slides-per-view="4" :space-between="55" :modules="modules" :loop="true" :navigation="false"
  67. :pagination="true" class="pos-relative" @swiper="onVerticalSwiper"
  68. >
  69. <SwiperSlide v-for="(item, index) in similarGoodsList" :key="index">
  70. <common-goods-item :item="item" />
  71. </SwiperSlide>
  72. </Swiper>
  73. </div>
  74. </div>
  75. </div>
  76. </template>
  77. <style lang='less' scoped>
  78. </style>