fengchuanyu 1 день тому
батько
коміт
6880b3eb71
2 змінених файлів з 105 додано та 0 видалено
  1. 16 0
      9_Vue/js/ajax.js
  2. 89 0
      9_Vue/练习题2_商品卡.html

+ 16 - 0
9_Vue/js/ajax.js

@@ -0,0 +1,16 @@
+function ajax(method, url,foo) {
+    let xhr = new XMLHttpRequest();
+    xhr.open(method, url);
+    xhr.send();
+    xhr.onreadystatechange = function () {
+        if (xhr.readyState === 4) {
+            if (xhr.status === 200) {
+                // console.log(xhr.responseText);
+                let data = JSON.parse(xhr.responseText);
+                foo(data);
+                // return data;
+            }
+        }
+    }
+
+}

+ 89 - 0
9_Vue/练习题2_商品卡.html

@@ -0,0 +1,89 @@
+<!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>
+    <script src="./js/ajax.js"></script>
+    <style>
+        .product-card {
+            width: 234px;
+            /* 如果数值为0 那么单位可以省略 如果值是0.X 0也可以省略 0.5=.5 */
+            box-shadow: 0 0 10px rgba(0, 0, 0, .5);
+            padding: 10px;
+            text-align: center;
+            float: left;
+            margin-right: 20px;
+        }
+
+        .product-img img {
+            width: 100%;
+        }
+
+        .product-title {
+            /* 文字大小尽量不要小于12px */
+            font-size: 14px;
+            font-weight: 400;
+            color: #333;
+        }
+
+        .product-info {
+            font-size: 12px;
+            color: #b0b0b0;
+            text-wrap: nowrap;
+            overflow: hidden;
+            text-overflow: ellipsis;
+            margin-top: 10px;
+            margin-bottom: 20px;
+        }
+
+        .product-price span {
+            color: #ff6700;
+        }
+
+        .product-price del {
+            color: #b0b0b0;
+        }
+    </style>
+</head>
+
+<body>
+    <div id="app">
+        <div class="btn">
+            <button v-on:click="getProduct">获取商品</button>
+        </div>
+        <div class="product-list">
+            <div class="product-card" v-for="item in productList">
+                <div class="product-img">
+                    <img :src="item.pic" alt="img">
+                </div>
+                <div class="product-title">{{item.prodName}}</div>
+                <div class="product-info">{{item.brief}}</div>
+                <div class="product-price">
+                    <span>{{item.price}}</span>
+                </div>
+            </div>
+        </div>
+    </div>
+    <script>
+        new Vue({
+            el: "#app",
+            data: {
+                productList: []
+            },
+            methods: {
+                getProduct() {
+                    let _this = this;
+                    ajax("GET","http://shop-api.edu.koobietech.com/prod/tagProdList",function(res){
+                        console.log(res.data[0].productDtoList);
+                        _this.productList = res.data[0].productDtoList;
+                    })
+                }
+            }
+        })
+    </script>
+</body>
+
+</html>