fengchuanyu 21 hours ago
parent
commit
6845c76f47
5 changed files with 141 additions and 4 deletions
  1. 1 1
      .vscode/settings.json
  2. 1 1
      8_ES6/练习题6_eventloop.html
  3. 21 2
      9_vue/10_watch.html
  4. 33 0
      9_vue/js/ajax.js
  5. 85 0
      9_vue/练习3_商品列表.html

+ 1 - 1
.vscode/settings.json

@@ -1,3 +1,3 @@
 {
-    "liveServer.settings.port": 5501
+    "liveServer.settings.port": 5502
 }

+ 1 - 1
8_ES6/练习题6_eventloop.html

@@ -17,7 +17,7 @@
         }, 0);
         console.log('3');
 
-        // 第二题 1 5 4 2 3
+        // 第二题 
 
         console.log('1');
         setTimeout(function () {

+ 21 - 2
9_vue/10_watch.html

@@ -8,7 +8,7 @@
 <body>
     <div id="app">
         <div>{{num}}</div>
-        <div>{{arr}}</div>
+        <div>{{obj.c.x}}</div>
         <input type="text" v-model.number="num">
         <button @click="change">按钮</button>
     </div>
@@ -18,7 +18,15 @@
             el:"#app",
             data:{
                 num:0,
-                arr:[1,2,3,4]
+                arr:[1,2,3,4],
+                obj:{
+                    a:1,
+                    b:2,
+                    c:{
+                        x:100,
+                        y:200
+                    }
+                }
             },
             methods:{
                 change(){
@@ -26,6 +34,8 @@
                     // this.arr.push(5);
                     // this.arr[1] = "hello";
                     // this.arr = ["a","b","c"];
+                    // this.$set(this.arr[2],0,"hello");
+                    this.obj.c.x = 1000;
                 }
             },
             watch:{
@@ -48,6 +58,15 @@
                 // arr(){
                 //     console.log("arr变化了");
                 // }
+                // obj(newVal,oldVal){
+                //     console.log(newVal,oldVal);
+                // }
+                obj:{
+                    handler(){
+                        console.log("obj变化了");
+                    },
+                    deep:true
+                }
             }
         })
     </script>

+ 33 - 0
9_vue/js/ajax.js

@@ -0,0 +1,33 @@
+function ajax(method, url, callback) {
+    // ajax 异步请求
+    // 第一步 实例化一个XMLHttpRequest对象
+    let xhr = new XMLHttpRequest();
+    // 第二步 调用open方法 配置请求参数
+    // open方法参数说明
+    // 第一个参数:请求方法 GET/POST
+    // 第二个参数:请求地址
+    // 第三个参数:是否异步 true/false
+    xhr.open(method, url, true);
+    // 第三步 调用send方法 发送请求
+    xhr.send();
+    // 第四步 监听状态变化
+    xhr.onreadystatechange = function () {
+        // 状态码 0 1 2 3 4
+        // 0: 请求未初始化 1: 服务器连接已建立 2: 请求已接收 3: 请求处理中 4: 请求已完成,且响应已就绪
+        if (xhr.readyState == 4) {
+            // 状态码 200 表示请求成功
+            if (xhr.status == 200) {
+                // 响应体
+                // console.log(xhr.responseText);
+                // 解析JSON字符串 转换为对象 调用JSON.parse方法
+                let jsonObj = JSON.parse(xhr.responseText);
+                // console.log(jsonObj);
+                // 访问对象属性
+                // console.log(jsonObj[0].url);
+
+                // 成功之后执行传递过来的回调函数
+                callback(jsonObj);
+            }
+        }
+    }
+}

+ 85 - 0
9_vue/练习3_商品列表.html

@@ -0,0 +1,85 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+    <style>
+        /* css reset */
+        *{
+            margin: 0;
+            padding: 0;
+        }
+        li{
+            list-style: none;
+            width: 200px;
+            padding: 30px;
+        }
+        ul{
+            display: flex;
+            flex-wrap: wrap;
+        }
+        li{
+           box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); 
+           margin: 30px;
+        }
+        .goods-img img{
+            width: 200px;
+        }
+        .goods-title{
+            font-size: 20px;
+            margin-top: 10px;
+            width: 100%;
+            overflow: hidden;
+            white-space: nowrap;
+            text-overflow: ellipsis;
+        }
+        .goods-desc,.goods-price{
+            margin-top: 10px;
+        }
+    </style>
+</head>
+<body>
+    <div id="app">
+        <ul>
+            <li v-for="item in goodsList">
+                <div class="goods-img">
+                    <img v-bind:src="item.pic" alt="商品图片">
+                </div>
+                <h2 class="goods-title">{{item.prodName}}</h2>
+                <p class="goods-desc">{{item.brief}}</p>
+                <p class="goods-price">¥{{item.price}}</p>
+            </li>
+        </ul>
+    </div>
+    <script src="./js/vue.js"></script>
+    <script src="./js/ajax.js"></script>
+    <script>
+        new Vue({
+            el: '#app',
+            data:{
+                goodsList:[]
+            },
+            created(){
+                // 调用获取商品列表方法
+                this.getGoodsList();
+            },
+            methods:{
+                // 获取商品列表
+                getGoodsList(){
+                    // let _this = this;
+                    // 发送ajax请求
+                    ajax("GET","http://shop-api.edu.koobietech.com/prod/tagProdList",(res)=>{
+                        // 处理响应数据
+                        let _goodsList = res.data[0].productDtoList;
+                        // console.log(_goodsList);
+                        // console.log(_this)
+                        // console.log(this);
+                        this.goodsList = _goodsList;
+                    })
+                }
+            }
+        })
+    </script>
+</body>
+</html>