当前位置:   article > 正文

Vue3+Vant4 电商平台-首页面详细教程_vant4官网

vant4官网

电商平台首页面制作

1. 技术点:

使用Vue3+Vant4 实现首页面的制作

2. 参考手册

Vant 4 官网:https://vant-ui.github.io/vant/#/zh-CN/

3. 页面效果

       

4.具体操作步骤

4.1. 创建Vue3 项目

     如果不会创建,之前文章有详细讲解,请查看

4.2. 安装Vue-Router / Vant

   npm install vue-router

   npm install vant

4.3 具体代码

4.3.1 商品模块组件(src/components/GoodsItem.vue)

  1. <template>
  2. <div class="goods-item">
  3. <div class="left">
  4. <img src="https://img14.360buyimg.com/n0/jfs/t1/65462/30/23386/129313/642141c0Fc8017a0f/75d08b31b05557ea.jpg" alt="" />
  5. </div>
  6. <div class="right">
  7. <p class="tit text-ellipsis-2">
  8. 维辰思 便携式显示器2K高刷4K触摸屏
  9. </p>
  10. <p class="count">已售100件</p>
  11. <p class="price">
  12. <span class="new">¥250</span>
  13. <span class="old">¥30</span>
  14. </p>
  15. </div>
  16. </div>
  17. </template>
  18. <script setup>
  19. </script>
  20. <style>
  21. .goods-item {
  22. height: 148px;
  23. margin-bottom: 6px;
  24. padding-left: 10px;
  25. padding-right: 10px;
  26. padding-top: 5px;
  27. background-color: #fff;
  28. display: flex;
  29. }
  30. .left{
  31. width: 127px;
  32. }
  33. .left img{
  34. display: block;
  35. width: 100%;
  36. }
  37. .right{
  38. flex: 1;
  39. font-size: 14px;
  40. line-height: 1.3;
  41. padding: 10px;
  42. display: flex;
  43. flex-direction: column;
  44. justify-content: space-evenly;
  45. }
  46. .right .count{
  47. color: #999;
  48. font-size: 12px;
  49. }
  50. .price{
  51. color: #999;
  52. font-size: 16px;
  53. }
  54. .price .new{
  55. color: #f03c3c;
  56. margin-right: 10px;
  57. }
  58. .price .old{
  59. text-decoration: line-through;
  60. font-size: 12px;
  61. }
  62. </style>

4.3.2 首页面具体代码(src/views/Index.vue)

  1. <template>
  2. <div class="index">
  3. <!--标题栏-->
  4. <van-nav-bar title="老马商城"></van-nav-bar>
  5. <!--搜索框-->
  6. <van-search
  7. background="#f1f1f2"
  8. placeholder="请输入搜索关键词" />
  9. <!--轮播图-->
  10. <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
  11. <van-swipe-item>
  12. <img src="@/assets/1.jpg"/>
  13. </van-swipe-item>
  14. <van-swipe-item>
  15. <img src="@/assets/2.jpg"/>
  16. </van-swipe-item>
  17. <van-swipe-item>
  18. <img src="@/assets/3.jpg"/>
  19. </van-swipe-item>
  20. </van-swipe>
  21. <!--分类列表-->
  22. <van-grid :border="false" :column-num="4">
  23. <van-grid-item v-for="item in categoryData">
  24. <van-image
  25. :src="item.imgpath"
  26. />
  27. <p>{{ item.title }}</p>
  28. </van-grid-item>
  29. </van-grid>
  30. <!--广告图-->
  31. <div class="main">
  32. <img src="@/assets/ad.jpg"/>
  33. </div>
  34. <!--商品列表-->
  35. <div class="guess">
  36. <p class="guess-like">—— 热门商品推荐 ——</p>
  37. <div class="goods-list">
  38. <GoodsItem v-for="item in 2"></GoodsItem>
  39. </div>
  40. </div>
  41. </div>
  42. <!--导航菜单-->
  43. <div>
  44. <router-view></router-view>
  45. <van-tabbar active-color="#ee0a24" inactive-color="#000">
  46. <van-tabbar-item icon="wap-home-o">首页</van-tabbar-item>
  47. <van-tabbar-item icon="apps-o">分类页</van-tabbar-item>
  48. <van-tabbar-item icon="shopping-cart-o">购物车</van-tabbar-item>
  49. <van-tabbar-item icon="user-o">我的</van-tabbar-item>
  50. </van-tabbar>
  51. </div>
  52. </template>
  53. <script setup>
  54. import GoodsItem from "@/components/GoodsItem.vue";
  55. import { reactive } from "vue";
  56. const categoryData = reactive([
  57. {
  58. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/187640/12/30456/5256/639c2315Ebc95c142/350a8f0c766f5460.png',
  59. title: '超市'
  60. },
  61. {
  62. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/149144/13/37547/3299/64b11027F86659ec1/fade439ac90544b7.png',
  63. title: '电器'
  64. },
  65. {
  66. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/177902/16/13776/5658/60ec0e71E801087f2/a0d5a68bf1461e6d.png',
  67. title: '海鲜'
  68. },
  69. {
  70. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/208529/13/35972/4081/6497ee53F4f8d38d9/c5267cb385f923fe.png',
  71. title: '充值'
  72. },
  73. {
  74. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/34248/39/16616/4689/62bbbdccE9f11132e/d51caf15f2f412b2.png',
  75. title: '附近好店'
  76. },
  77. {
  78. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/37709/6/15279/6118/60ec1046E4b5592c6/a7d6b66354efb141.png',
  79. title: 'PLUS会员'
  80. },
  81. {
  82. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/97574/40/22764/2060/64a4ff80Fd550d65e/eeea8b95458cc7fd.png',
  83. title: '到家'
  84. },
  85. {
  86. imgpath: 'https://m15.360buyimg.com/mobilecms/jfs/t1/195186/28/35854/3469/64d4a1e5Fe388886e/3c6f65ecd0aba98b.png',
  87. title: '服饰美妆'
  88. },
  89. ])
  90. </script>
  91. <style>
  92. .index{
  93. padding-bottom: 50px;
  94. }
  95. .van-nav-bar{
  96. background-color: #c21401;
  97. }
  98. .my-swipe .van-swipe-item {
  99. height: 185px;
  100. color: #fff;
  101. font-size: 20px;
  102. text-align: center;
  103. background-color: #39a9ed;
  104. }
  105. .my-swipe .van-swipe-item img{
  106. width: 100%;
  107. height: 185px;
  108. }
  109. .van-grid{
  110. padding-top: 18px;
  111. }
  112. .van-grid .van-grid-item img{
  113. width:40px;
  114. height:40px;
  115. }
  116. .van-grid .van-grid-item p{
  117. font-size: 15px
  118. }
  119. .van-grid-item{
  120. height: 75px;
  121. }
  122. .main img{
  123. width: 100%;
  124. height:200px
  125. }
  126. .guess .guess-like {
  127. height: 40px;
  128. line-height: 40px;
  129. text-align: center;
  130. }
  131. </style>

4.3.3 路由配置(src/router/index.js)

  1. import {createRouter,createWebHistory} from 'vue-router'
  2. const router = createRouter({
  3. history: createWebHistory(),
  4. routes: [
  5. {
  6. path: '/',
  7. redirect: '/index'
  8. },
  9. {
  10. path: '/index',
  11. name: 'index',
  12. component: () => import('@/views/Index.vue')
  13. }
  14. ]
  15. })
  16. export default router

4.3.4 主文件(main.js)

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. import Router from './router/index'
  4. //-vant配置
  5. import Vant from 'vant'
  6. import 'vant/lib/index.css'
  7. const app = createApp(App)
  8. app.use(Router)
  9. app.use(Vant)
  10. app.mount('#app')

4.3.5 主页面(App.vue)

  1. <template>
  2. <RouterView></RouterView>
  3. </template>

到此,整个案例整理完毕!

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

闽ICP备14008679号