鱼C论坛

 找回密码
 立即注册
查看: 2935|回复: 2

关于vue组件的事件问题

[复制链接]
发表于 2019-3-27 15:41:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我现在在做一个电商类型网站。  
采取django作为后端。  
在购物车页面方面采取了vue.js来辅助渲染效果。  
页面如下:
屏幕快照 2019-03-27 15.36.36.png
目前在商品的数量方面出现问题。
我想点击加号的时候,商品加一。
但是组件化后,该怎么选择到数量这个对象来加一操作
代码如下:
  1. <script type="text/javascript">
  2. function getCookie (name) {
  3.       var value = '; ' + document.cookie
  4.       var parts = value.split('; ' + name + '=')
  5.       if (parts.length === 2) return parts.pop().split(';').shift()
  6.   }

  7. Vue.component('button-counter', {
  8.   delimiters: ['$vue{', '}'],
  9.   props: ['post'],
  10.   template:`
  11.   <tr >
  12.     <th scope="row">$vue{post.title}</th>
  13.     <td>$vue{post.title}</td>
  14.     <td>$vue{post.price}</td>
  15.     <td>

  16.     <form class="" action="index.html" method="post">
  17.       <p class="form-inline"">
  18.             <a type="button" class="btn btn-secondary">-</a>
  19.           <input v-model="post.nums" type="text"  class="form-control site-product-input" size="4" maxlength="6"  aria-describedby="btnGroupAddon">

  20.             <a type="button" v-on:click="$emit('add_nums')" class="btn btn-secondary">+</a>
  21.       </p>

  22.     </form>

  23.     </td>
  24.     <td>$vue{post.nums * post.price}</td>
  25.   </tr>
  26.   `
  27. })

  28. var headerss = {
  29.         'X-CSRFToken': getCookie('csrftoken')
  30.         }
  31. new Vue({
  32.   el: '#components-demo',
  33.   delimiters: ['$vue{', '}'],
  34.   data: {
  35.     posts: [
  36.       {% for item in product %}
  37.       {
  38.         id:{{item.id}},
  39.         slug:'{{ item.product.product_uuid }}',
  40.         title:'{{ item.product.title }}',
  41.         price:{{ item.product.price }},
  42.         in_stock:{{ item.product.in_stock }},
  43.         img_path:'{{ item.product.image_path }}',
  44.         nums:{{ item.nums }}


  45.       },
  46.       {% endfor %}
  47.     ],
  48.   headers:headerss
  49. },
  50. methods: {
  51.   add_num: function (event) {
  52.     if (this.post.nums === this.in_stock) {
  53.       this.post.nums = 1
  54.     } else {
  55.       this.post.nums += 1;
  56.     }


  57.   },
  58.   minus_num: function (event) {
  59.     if (this.nums === 1  ) {
  60.       this.post.nums = this.post.in_stock
  61.     } else {
  62.       this.nums -= 1;
  63.     }


  64.   },
  65.   add_to_cart: function (event) {
  66.     axios({
  67.     url: this.urls,
  68.     method: "POST",
  69.     headers: this.headers,
  70.     // withCredentials: true,
  71.     data: { "nums": this.nums, "product_id": this.product_id }
  72.   }).then(response => {

  73.     console.log(response);
  74.   }).catch(err => {
  75.     console.log(err);
  76.   });

  77.   }
  78. }

  79. })
复制代码

html代码如下:
  1. <div id="components-demo">
  2. <table class="table">
  3.   <thead>
  4.     <tr>
  5.       <th scope="col">product</th>
  6.       <th scope="col">img</th>
  7.       <th scope="col">unit price</th>
  8.       <th scope="col">nums</th>
  9.       <th scope="col">Total price</th>

  10.     </tr>
  11.   </thead>
  12.   <tbody is='button-counter' v-for="post in posts"
  13.   v-bind:post="post"
  14.   v-bind:key="post.id"
  15.   v-on:add_nums="add_num"
  16.   >



  17.   </tbody>
  18. </table>
  19.   </div>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-27 16:51:18 | 显示全部楼层
data的数据如下:
  1. data: {
  2.     posts: [
  3.       
  4.       {
  5.         id:7,
  6.         slug:'11111111',
  7.         title:'banana',
  8.         price:50.0,
  9.         in_stock:100,
  10.         img_path:'img/6.jpg',
  11.         nums:5


  12.       },
  13.       
  14.       {
  15.         id:9,
  16.         slug:'rrrr',
  17.         title:'huwr',
  18.         price:100.0,
  19.         in_stock:1100,
  20.         img_path:'img/88.jpg',
  21.         nums:18


  22.       },
  23.       
  24.     ],
  25.   headers:headerss
  26. },
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 19:53:58 | 显示全部楼层
解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-5 19:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表