zheng 4 дней назад
Родитель
Сommit
43c266cd2d
3 измененных файлов с 206 добавлено и 0 удалено
  1. 82 0
      vue/初阶/15.列表排序.html
  2. 47 0
      vue/初阶/16.数据代理.html
  3. 77 0
      vue/初阶/17.数据监听.html

+ 82 - 0
vue/初阶/15.列表排序.html

@@ -0,0 +1,82 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+
+<body>
+    <div id="app">
+        关键字搜索:
+        <input type="text" placeholder="请输入关键字" v-model="keywords">
+        <br>
+        <ul v-for="(item,index) in newList" :key="index">
+            <li>{{index+1}}.我叫{{item.name}}--今年{{item.age}}了--我是{{item.sex}}孩</li>
+        </ul>
+        <button @click="sortType = 1">升序</button>
+        <button @click="sortType = 2">降序</button>
+        <button @click="sortType = 0">默认排序</button>
+    </div>
+    <script src="./vue.js"></script>
+    <script>
+        var app = new Vue({
+            el: "#app",
+            data: {
+                sortType:0,
+                keywords:"",
+                arr: [
+                    {
+                        name: "Lucy",
+                        age: 11,
+                        sex: "女",
+                    },
+                    {
+                        name: "LiLi",
+                        age: 31,
+                        sex: "男",
+                    },
+                    {
+                        name: "八戒",
+                        age: 21,
+                        sex: "男",
+                    },
+                    {
+                        name: "八卦",
+                        age: 23,
+                        sex: "男",
+                    },
+                    {
+                        name: "孙悟空",
+                        age: 27,
+                        sex: "男",
+                    },
+                    {
+                        name: "唐僧",
+                        age: 33,
+                        sex: "女",
+                    },
+                ],
+                personList:[]
+            },
+            computed: {
+                newList() {
+                   this.personList =this.arr.filter((item => {
+                        return item.name.indexOf(this.keywords) !== -1;
+                    }));
+
+                    if(this.sortType) {
+                        this.personList.sort((a,b) => {
+                            return this.sortType == 1 ? a.age - b.age : b.age - a.age;
+                        })
+                    }
+
+                    return this.personList;
+                }
+            }
+        })
+    </script>
+</body>
+
+</html>

+ 47 - 0
vue/初阶/16.数据代理.html

@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <!-- 
+        数据代理:
+        Object.defineProperty()中getter和setter
+    -->
+          <div id="app">
+        <h1>{{msg}}</h1>
+    </div>
+    <script src="./vue.js"></script>
+    <script>
+        var app = new Vue({
+            el: "#app",
+            data:{
+                msg:'12'
+            }
+        })
+    </script>
+    <script>
+        let obj = {
+            name:"图图",
+            age: 3
+        }
+        let x = 31;
+        // console.log(obj.name)
+        Object.defineProperty(obj,'age',{
+            get() {
+                console.log("读取");
+                return x;
+            },
+            set(value) {
+                console.log("修改")
+                x = value;
+            }
+        })
+        obj.age = 12;
+        console.log(obj.age)
+        
+    </script>
+</body>
+</html>

+ 77 - 0
vue/初阶/17.数据监听.html

@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+
+<body>
+    <div id="app">
+        <button @click="changeName">修改第二项的名字</button>
+        <ul v-for="(item,index) in arr" :key="index">
+            <li>{{index+1}}.我叫{{item.name}}--今年{{item.age}}了--我是{{item.sex}}孩</li>
+        </ul>
+    </div>
+    <script src="./vue.js"></script>
+    <script>
+        var app = new Vue({
+            el: "#app",
+            data: {
+                arr: [
+                    {
+                        name: "Lucy",
+                        age: 11,
+                        sex: "女",
+                    },
+                    {
+                        name: "LiLi",
+                        age: 31,
+                        sex: "男",
+                    },
+                    {
+                        name: "八戒",
+                        age: 21,
+                        sex: "男",
+                    },
+                    {
+                        name: "八卦",
+                        age: 23,
+                        sex: "男",
+                    },
+                    {
+                        name: "孙悟空",
+                        age: 27,
+                        sex: "男",
+                    },
+                    {
+                        name: "唐僧",
+                        age: 33,
+                        sex: "女",
+                    },
+                ],
+            },
+            /**
+             * Vue2响应式:Object.defineProperty()实现
+             * 它只能监听对象属性的修改 无法监听数组下标直接赋值
+            */
+            methods: {
+                changeName() {
+                    // this.arr[1].name = '图图';
+                    // this.arr[1] = { name: "图图", age: 3 }
+                    this.$set(this.arr,1,{ name: "图图", age: 3 })
+                    // Vue.set(this.arr,1,{ name: "图图", age: 3 })
+                    console.log(this.arr, 'arr')
+                }
+            },
+            computed:{},
+            watch:{},
+            created() {
+                
+            }
+        })
+    </script>
+</body>
+
+</html>