练习题5_简易购物车.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="./js/vue.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <h1>商品单价:{{price}}</h1>
  12. <div>购买数量:<input type="number" v-model="num"></div>
  13. <h1>{{msg}}</h1>
  14. <h1>商品总价:{{totalPrice}}</h1>
  15. <h1>会员9折后价格:{{memberPrice}}</h1>
  16. </div>
  17. <script>
  18. new Vue({
  19. el: '#app',
  20. data: {
  21. price:200,
  22. num:0,
  23. msg:"还剩余10件"
  24. },
  25. computed:{
  26. totalPrice(){
  27. return this.price*this.num;
  28. },
  29. memberPrice(){
  30. return this.totalPrice*0.9;
  31. }
  32. },
  33. watch:{
  34. num(newVal,oldVal){
  35. if(newVal<0){
  36. this.num = 0;
  37. }else if(newVal>10){
  38. this.msg= "购买数量不能超过10件";
  39. // this.num = 10;
  40. }else{
  41. this.msg="还剩余"+(10-newVal)+"件"
  42. }
  43. }
  44. }
  45. })
  46. </script>
  47. </body>
  48. </html>