fengchuanyu 1 week ago
parent
commit
bc1c97727f
1 changed files with 49 additions and 0 deletions
  1. 49 0
      9_Vue/练习题5_简易购物车.html

+ 49 - 0
9_Vue/练习题5_简易购物车.html

@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+    <script src="./js/vue.js"></script>
+</head>
+<body>
+    <div id="app">
+        <h1>商品单价:{{price}}</h1>
+        <div>购买数量:<input type="number" v-model="num"></div>
+        <h1>{{msg}}</h1>
+        <h1>商品总价:{{totalPrice}}</h1>
+        <h1>会员9折后价格:{{memberPrice}}</h1>
+    </div>
+    <script>
+        new Vue({
+            el: '#app',
+            data: {
+                price:200,
+                num:0,
+                msg:"还剩余10件"
+            },
+            computed:{
+                totalPrice(){
+                    return this.price*this.num;
+                },
+                memberPrice(){
+                    return this.totalPrice*0.9;
+                }
+                
+            },
+            watch:{
+                num(newVal,oldVal){
+                    if(newVal<0){
+                        this.num = 0;
+                    }else if(newVal>10){
+                        this.msg= "购买数量不能超过10件";
+                        // this.num = 10;
+                    }else{
+                        this.msg="还剩余"+(10-newVal)+"件"
+                    }
+                }
+            }
+        })
+    </script>
+</body>
+</html>