• 商品详情页
    • 1. 新增路由
    • 2. 新增vue页面
    • 3. 添加物品到购物车
    • 看效果
    • 总结

    商品详情页

    当用户在商品列表页中点击时,就会跳转到该页面。

    步骤如下:

    1. 新增路由

    向src/router/index.js中增加:

    1. import GoodsDetails from '@/views/shops/goods_details'
    2. Vue.use(Router)
    3. export default new Router({
    4. routes: [
    5. {
    6. path: '/shops/goods_details',
    7. name: 'GoodsDetails',
    8. component: GoodsDetails
    9. },
    10. ]
    11. })

    2. 新增vue页面

    向 src/views/shops/goods_details 中增加:

    1. <template>
    2. <div class="background">
    3. <div class="goods_detail" style="height: 100%;">
    4. <header class="top_bar">
    5. <a onclick="window.history.go(-1)" class="icon_back"></a>
    6. <h3 class="cartname">商品详情</h3>
    7. </header>
    8. <div class="tast_list_bd" style="padding-top: 44px;">
    9. <main class="detail_box">
    10. <!-- 轮播图 -->
    11. <div class="home_ban">
    12. <div class="m_banner clearfix" id="my_banner">
    13. <ul class="banner_box" >
    14. <div v-for="image in good_images">
    15. <li><img :src="image" alt="" style="height: 300px"/></li>
    16. </div>
    17. </ul>
    18. <ul class="point_box" >
    19. <li></li>
    20. </ul>
    21. </div>
    22. </div>
    23. <section class="product_info clearfix">
    24. <div class="product_left">
    25. <p class="p_name">{{good.name}}</p>
    26. <div class="product_pric">
    27. <span></span>
    28. <span class="rel_price">{{good.price}}</span>
    29. <span></span>
    30. <span style='color: grey;
    31. text-decoration: line-through;
    32. font-size: 18px;
    33. margin-left: 14px;'>
    34. 原价: ¥{{good.original_price}}
    35. </span>
    36. </div>
    37. <!--
    38. <div class="product_right">
    39. 降价通知
    40. </div>
    41. -->
    42. </div>
    43. </section>
    44. <span class="divider" style="height: 2px;"></span>
    45. <div id="choose_number" style= "height: 40px; background-color: #fff;">
    46. <label style="font-size: 18px; float: left; margin-left: 10.5px; margin-top: 7.5px;">购买数量</label>
    47. <div style= "padding-top: 5px;">
    48. <img src="../../assets/add@2x.png" style="margin-right: 10px; display: inline;float:right;width:30px;" class="plus" @click="plus"/>
    49. <input pattern="[0-9]*" v-model="buy_count" type="text" name="counts" style="width:30px;display: inline;float:right;border: 0.5px solid #e2e2e2;line-height:28px;text-align:center;"/>
    50. <img src="../../assets/minus@2x.png" style="display: inline;float:right;width:30px;" class="minus" @click="minus"/>
    51. </div>
    52. </div>
    53. <section class="product_intro">
    54. <div class="pro_det" v-html="good.description" style='padding: 0 6.5px;'>
    55. </div>
    56. </section>
    57. </main>
    58. </div>
    59. <footer class="cart_d_footer">
    60. <div class="m">
    61. <ul class="m_box">
    62. <li class="m_item">
    63. <a @click="toCart" class="m_item_link">
    64. <em class="m_item_pic three"></em>
    65. <span class="m_item_name">购物车</span>
    66. </a>
    67. </li>
    68. </ul>
    69. <div class="btn_box clearfix" >
    70. <a @click="addToCart" class="buy_now">加入购物车</a>
    71. <a @click="zhifu" class="buybuy">立即购买</a>
    72. </div>
    73. </div>
    74. </footer>
    75. </div>
    76. </div>
    77. </template>
    78. <script>
    79. import { go } from '../../libs/router'
    80. //import { swiper, swiperSlide } from 'vue-awesome-swiper'
    81. import {scrollPic} from '../../libs/index.js'
    82. export default{
    83. data(){
    84. return {
    85. good_images: [],
    86. good: "",
    87. buy_count: 1,
    88. good_id: this.$route.query.good_id,
    89. }
    90. },
    91. watch:{
    92. },
    93. mounted(){
    94. scrollPic(); //轮播图
    95. this.$http.get(this.$configs.api + 'goods/goods_details?good_id=' + this.good_id).then((response)=>{
    96. console.info(this.good_id)
    97. console.info(response.body)
    98. this.good = response.body.good
    99. this.good_images = response.body.good_images
    100. },(error) => {
    101. console.error(error)
    102. });
    103. },
    104. methods:{
    105. addToCart () {
    106. alert("商品已经加入到了购物车")
    107. let goods = {
    108. id: this.good_id,
    109. title: this.good.name,
    110. quantity: this.buy_count,
    111. price: this.good.price,
    112. image: this.good_images[0]
    113. }
    114. this.$store.dispatch('addToCart', goods)
    115. },
    116. toCart () {
    117. go("/cart", this.$router)
    118. },
    119. plus () {
    120. this.buy_count = this.buy_count + 1
    121. },
    122. minus () {
    123. if(this.buy_count > 1) {
    124. this.buy_count = this.buy_count - 1
    125. }
    126. },
    127. zhifu () {
    128. go("/shops/dingdanzhifu?good_id=" + this.good_id + "&buy_count=" + this.buy_count, this.$router)
    129. },
    130. },
    131. components: {
    132. },
    133. computed: {
    134. }
    135. }
    136. </script>

    在上面的代码中,

    1. 实现了加入购物车的方法
    2. 实现了对于支付页面的跳转
    3. 实现了从远程接口读取数据

    3. 添加物品到购物车

    下面的代码是把某个商品添加到购物车中:

    1. addToCart () {
    2. console.info('加入购物车')
    3. alert("商品已经加入到了购物车")
    4. let goods = {
    5. id: this.good_id,
    6. title: this.good.name,
    7. quantity: this.buy_count,
    8. price: this.good.price,
    9. image: this.good_images[0]
    10. }
    11. this.$store.dispatch('addToCart', goods)
    12. },

    同时,在 src/vuex/actions.js中,添加如下代码:

    1. export const addToCart = ({ commit }, product) => {
    2. commit(types.ADD_TO_CART, {
    3. id: parseInt(product.id),
    4. image: product.image,
    5. title: product.title,
    6. quantity: product.quantity,
    7. price: product.price
    8. })
    9. }

    看效果

    详情页

    总结

    • 购物车使用了Vuex来保存数据。 下一节会详述。
    • 进入到支付页面,在后面会详述。 这个页面我们只加上一个链接就好了
    • 本页面使用了后台提供的接口,会返回必要的数据。 接口结构略。