vue03.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板</title>
  6. <!-- 引入文件 -->
  7. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  8. <style>
  9. .active {
  10. height: 200px;
  11. width: 200px;
  12. border: 1px solid red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="app">
  18. <h1>图书列表</h1>
  19. <table width="80%" cellspacing="10" cellpadding="0" >
  20. <tr>
  21. <th>图书编号</th>
  22. <th>图书名称</th>
  23. <th>图书价格</th>
  24. <th>图书作者</th>
  25. <th>数量</th>
  26. <th>操作</th>
  27. </tr>
  28. <tr v-for="(item, index) in books " :key="item.id" align="center" >
  29. <td>{{item.id}}</td>
  30. <td>{{item.name}}</td>
  31. <td>{{item.price}}</td>
  32. <td>{{item.author}}</td>
  33. <td>
  34. <button @click="subBookCount(index)" >-</button>
  35. {{item.count}}
  36. <button @click="item.count++" >+</button>
  37. </td>
  38. <td>
  39. <button @click="delBooks(index)" >删除</button>
  40. </td>
  41. </tr>
  42. <h3>总价格:{{totalPrice}}</h3>
  43. </table>
  44. </div>
  45. </body>
  46. <script>
  47. const { createApp, ref , computed } = Vue;
  48. createApp({
  49. setup() {
  50. //数组
  51. let books = ref([
  52. {id: 1, name: '神墓', price: 129.0, author: '辰东',count:1},
  53. {id: 2, name: '斗破', price: 199.0, author: '土豆',count:1},
  54. {id: 3, name: '大主宰', price: 119, author: '土豆',count:1},
  55. {id: 4, name: '遮天', price: 99, author: '辰东',count:1},
  56. {id: 5, name: '吞噬星空', price: 188, author: '番茄',count:1},
  57. {id: 6, name: '凡人修仙传', price: 129, author: '耳根',count:1},
  58. ])
  59. // 一个计算属性 ref 总价格
  60. const totalPrice = computed(() => {
  61. let sumPrice = 0;
  62. for (let i = 0; i < books.value.length; i++) {
  63. // console.log( (parseInt(books.value[i].count) * parseInt(books.value[i].price))) ;
  64. // console.log(books.value[i].count)
  65. // console.log(books.value[i].price)
  66. sumPrice += (parseInt(books.value[i].count) * parseInt(books.value[i].price))
  67. }
  68. return sumPrice
  69. })
  70. function subBookCount(index){
  71. //console.log(index)
  72. if (books.value[index].count == 0){
  73. books.value[index].count = 0;
  74. }else{
  75. books.value[index].count--;
  76. }
  77. }
  78. function delBooks(index){
  79. //splice 实现替换 删除 增加
  80. let book = {id: 6, name: '灵龙', price: 129, author: '一画开天',count:1}
  81. /*
  82. 索引位置
  83. 删除的个数
  84. 替换的元素 添加的元素
  85. */
  86. // 替换 books.value.splice(index,1,book)
  87. // 删除
  88. books.value.splice(index,1)
  89. }
  90. return {
  91. books,subBookCount,totalPrice,delBooks
  92. };
  93. }
  94. }).mount('#app'); //createApp创建对象 挂在到app
  95. </script>
  96. </html>