对比一下找不同

news/2024/7/10 0:52:33 标签: vue
<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>


http://www.niftyadmin.cn/n/957437.html

相关文章

算法学习(四)——alphago模型

模型推荐看原文&#xff1a; https://xueshu.baidu.com/usercenter/paper/show?paperida7600bdc74f5a07ed65256035cd15c6b&sitexueshu_se 自己的理解&#xff1a; MCTS解决的是算力分配的问题&#xff0c;alphago主要解决了五个问题&#xff1a; 一是把深度学习和蒙特…

什么叫回调函数?

什么叫回调函数&#xff1f;

算法学习(五)——alphago Zero模型

论文原文&#xff1a; https://xueshu.baidu.com/usercenter/paper/show?paperid2c541a0965ba18d2f7f835ecdbe4f37d&sitexueshu_se github上的复现&#xff0c;膜拜大神&#xff1a; https://github.com/junxiaosong/AlphaZero_Gomoku alphago Zero模型的理解&#xf…

numpy学习笔记1—ravel() 和 flatten()

numpy的ravel() 和 flatten()函数 简介 首先声明两者所要实现的功能是一致的&#xff08;将多维数组降位一维&#xff09;。这点从两个单词的意也可以看出来&#xff0c;ravel(散开&#xff0c;解开)&#xff0c;flatten&#xff08;变平&#xff09;。两者的区别在于返回拷贝…

promise的创建

const promise new Promise(function(resolve, reject){setTimeout(function(){try {let c 6 / 2 ;resolve(c);console.log(c)}catch(ex) {reject(ex);}}, 1000) });得出答案是3 但是把console.log放在最后一行会报错&#xff0c;说c没被定义 const promise new Promise(f…

Python3之max key参数学习记录

今天用Python写脚本&#xff0c;想要实现这样的功能&#xff1a;对于给定的字典&#xff0c;返回其中Value最大值对应的Key。 搜索后找到了解决方法&#xff0c;同时也学到了max key参数的作用。 例1&#xff0c; testlist [9.2, 10, -20.3, -7.0, 9.999, 20.111] print(ma…

promise的运行

const promise new Promise(function(resolve, reject){setTimeout(function(){try {let c 6 / 2 ;resolve(c);console.log(c)}catch(ex) {reject(ex);}}, 1000) }) promise.then(function(value) {console.log(value) },function(err){console.error(err.message) })运行结果…

说有bug,但是好像没有啊,没看出来哪里可以改

const promise new Promise((resolve, reject) > {setTimeout( () > {let inArray new Array(20);for(let i0; i<20; i) {intArray[i] parseInt(Math.random() * 20, 10);resolve(inArray);}, 1000);console.log("开始生成一个随机的数组")});promise.th…