对比一下找不同

628 篇文章 6 订阅
<template>
    <div>
        <table>
            <tr>
                <td>商品编号</td>
                <td><input type="text" v-model.number="id"></td>
            </tr>
            <tr>
                <td>商品名称</td>
                <td><input type="text" v-model.number="title"></td>
            </tr>
            <tr>
                <td>商品价格</td>
                <td><input type="text" v-model.number="price"></td>
            </tr>
             <tr>
                <td colspan="2"><button @click="addCart">加入购物车</button></td>
            </tr>
        </table>
        <table>
            <thead>
                <tr>
                    <th>编号</th>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    <th>金额</th>
                    <th>操作</th>
                </tr>
            </thead>
        <tbody>
            <tr v-for="book in books" :key="book.id">
                <td>{{ book.id }}</td>
                <td>{{ book.title }}</td>
                <td>{{ book.price }}</td>
                <td><button>-</button> {{ book.count }} <button>+</button>></td>
                <td>金额</td>
                <td><button>删除</button></td>
            </tr>
        </tbody>
        </table>
        <span>总价:¥0.00</span>
    </div>
</template>

<script>
export default {
    data() {
        return {
            id: null,
            title: '',
            price: '',
            quantity: 1
        }
    },
    computed: {
        books() {
            return this.$store.state.items;
        }
    },
    methods: {
        addCart() {
            this.$store.commit('pushItemToCart', {
                id: this.id,
                title: this.title,
                price: this.price,
                count: this.quartity
            })
            this.id = '';
            this.title = '';
            this.price = '';
        }
    }

};
</script>


<template>
  <div>
<table>
  <tr>
    <td>商品编号</td>
    <td><input type="text" v-model.number="id"></td>
  </tr>
  <tr>
    <td>商品名称</td>
    <td><input type="text" v-model="title"></td>
  </tr>
  <tr>
    <td>商品价格</td>
    <td><input type="text" v-model="price"></td>
  </tr>
  <tr>
    <td>数量</td>
    <td><input type="text" v-model.number="quantity"></td>
  </tr>
  <tr>
    <td colspan="2"><button @click="addCart">加入购物车</button></td>
  </tr>
</table>
    <table>
      <thead>
        <tr>
          <th>编号</th>
          <th>商品名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>金额</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="book in books" :key="book.id">
          <td>{{ book.id }}</td>
          <td>{{ book.title }}</td>
          <td>{{ book.price }}</td>
          <td>
            <button :disabled="book.count===0" @click="increment({id: book.id, count: -1})">-</button>
            {{ book.count }}
            <button @click="increment({id: book.id, count: 1})">+</button>
          </td>
          <td>{{ itemPrice(book.id) }}</td>
          <td><button @click="deleteItem(book.id)">删除</button></td>
        </tr>
      </tbody>
    </table>
    <span>总价:¥{{ totalPrice }}</span>
  </div>
</template>

<script>
import { mapMutations, mapState, mapGetters, mapActions } from 'vuex'
export default {
  data(){
    return {
      id: null,
      title: '',
      price: '',
      quantity: 1
    }
  },
  computed: {
    /* books(){
      return this.$store.state.items;
    } */
    ...mapState('cart', {
      books: 'items'
    }),
    ...mapGetters('cart', {
      itemPrice: 'cartItemPrice',
      totalPrice: 'cartTotalPrice'
    })
  },
  methods: {
    ...mapMutations('cart', {
      addItemToCart: 'pushItemToCart',
      increment: 'incrementItemCount'
    }),
    ...mapMutations('cart', [
      'deleteItem'
    ]),
    ...mapActions('cart', [
      'addItemToCart'
    ]),
    addCart(){
      //this.$store.commit('pushItemToCart', {
      /* this.addItemToCart({
        id: this.id,
        title: this.title,
        price: this.price,
        count: this.quantity
      }) */

      //this.$store.dispatch('addItemToCart', {
      this.addItemToCart({
        id: this.id,
        title: this.title,
        price: this.price,
        count: this.quantity
      })
      this.id = '';
      this.title = '';
      this.price = '';
    }
  }
};
</script>

<style scoped>
div {
  width: 800px;
}
table {
  border: 1px solid black;
  width: 100%;
  margin-top: 20px;
}
th {
  height: 50px;
}
th, td {
  border-bottom: 1px solid #ddd;
  text-align: center;
}
span {
  float: right;
}
</style>

为什么数量那里总是出来一个1

<template>
    <div>
        <table>
            <tr>
                <td>商品编号</td>
                <td><input type="text" v-model.number="id"></td>
            </tr>
            <tr>
                <td>商品名称</td>
                <td><input type="text" v-model.number="title"></td>
            </tr>
            <tr>
                <td>商品价格</td>
                <td><input type="text" v-model.number="price"></td>
            </tr>
            <tr>
                <td>数量</td>
                <td><input type="text" v-model.number="quantity"></td>
            </tr>
             <tr>
                <td colspan="2"><button @click="addCart">加入购物车</button></td>
            </tr>
        </table>
        <table>
            <thead>
                <tr>
                    <th>编号</th>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    <th>金额</th>
                    <th>操作</th>
                </tr>
            </thead>
        <tbody>
            <tr v-for="book in books" :key="book.id">
                <td>{{ book.id }}</td>
                <td>{{ book.title }}</td>
                <td>{{ book.price }}</td>
                <td><button :disabled="book.count===0" @click="increment({id: book.id, count: -1})">-</button>
                {{ book.count }} 
                <button @click="increment({id: book.id, count: 1})">+</button></td>
                <td>{{ itemPrice(book.id) }}</td>
                <td><button @click="deleteItem(book.id)">删除</button></td>
            </tr>
        </tbody>
        </table>
        <span>总价:¥{{ totalPrice }}</span>
    </div>
</template>

<script>
import { mapMutations, mapState, mapGetters, mapActions } from 'vuex'
export default {
    data() {
        return {
            id: null,
            title: '',
            price: '',
            quantity: ''
        }
    },
    computed: {
        ...mapState('cart', {
            books: 'items'
        }),
        ...mapGetters('cart', {
            itemPrice: 'cartItemPrice',
            totalPrice: 'cartTotalPrice'
        })
    },
    methods: {
        ...mapMutations('cart', {
            addItemToCart: 'pushItemToCart',
            increment: 'incrementItemCount'
        }),
        ...mapMutations('cart', [
            'deleteItem'
        ]),
        ...mapActions('cart', [
            'addItemToCart'
        ]),
        addCart(){
      
      this.addItemToCart({
        id: this.id,
        title: this.title,
        price: this.price,
        count: this.quantity
      })
      this.id = '';
      this.title = '';
      this.price = '';
        }
    }

};
</script>
<style scoped>
div {
  width: 800px;
}
table {
  border: 1px solid black;
  width: 100%;
  margin-top: 20px;
}
th {
  height: 50px;
}
th, td {
  border-bottom: 1px solid #ddd;
  text-align: center;
}
span {
  float: right;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值