zsydgithub 11 ヶ月 前
コミット
275ed65420
2 ファイル変更166 行追加0 行削除
  1. 109 0
      vue/5_todoList.html
  2. 57 0
      vue/6_set.html

+ 109 - 0
vue/5_todoList.html

@@ -0,0 +1,109 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+  <style>
+    #app {
+      width: 500px;
+    }
+
+    .close {
+      float: right;
+    }
+
+    .active {
+      background: red;
+    }
+  </style>
+</head>
+
+<body>
+  <div id="app">
+    <h2>todoList</h2>
+    <hr>
+    <div>
+      名称:<input type="text" v-model="name">
+      价格: <input type="text" v-model="price">
+      <button @click="add">提交</button>
+    </div>
+    <div>
+      <input type="text" v-model="searchVal">
+      <button @click="search">搜索</button>
+    </div>
+    <ul>
+      <li v-for="(obj,index) in list" v-show="obj.isShow" :class="{active: obj.isCheck}">
+        <input type="checkbox" v-model="obj.isCheck">
+        <span>{{obj.name}}</span>
+        <span>{{obj.price}}</span>
+        <span class="close" @click="del(index)">[X]</span>
+      </li>
+    </ul>
+    <div>
+      <button>删除选中</button>
+      <span>总价:{{total()}}</span>
+    </div>
+
+  </div>
+  <script src="vue.js"></script>
+  <script>
+    var app = new Vue({
+      el: '#app',
+      data: {
+        list: [{
+          name: '苹果',
+          price: 5,
+          isShow: true,
+          isCheck: false
+        }, {
+          name: '香蕉',
+          price: 8,
+          isShow: true,
+          isCheck: false
+        }],
+        name: '',
+        price: '',
+        searchVal: ''
+      },
+      methods: {
+        add() {
+          this.list.push({
+            name: this.name,
+            price: this.price,
+            isShow: true,
+            isCheck: false
+          })
+          this.name = ''
+          this.price = ''
+        },
+        search() {
+          this.list.forEach((obj) => {
+            if (obj.name.includes(this.searchVal)) {
+              obj.isShow = true
+            } else {
+              obj.isShow = false
+            }
+          })
+          this.searchVal = ''
+        },
+        del(index) {
+          this.list.splice(index, 1)
+        },
+        total() {
+          var num = 0
+          this.list.forEach((obj) => {
+            if (obj.isCheck) {
+              num += obj.price * 1
+            }
+          })
+          return num
+        }
+      }
+    })
+  </script>
+</body>
+
+</html>

+ 57 - 0
vue/6_set.html

@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+
+<body>
+  <div id="app">
+    <button @click="change">修改</button>
+    <ul>
+      <li v-for="(val,index) in arr">
+        {{val}}
+        {{index}}
+      </li>
+    </ul>
+  </div>
+  <script src="vue.js"></script>
+  <script>
+    /* 
+      Vue中 直接修改数组的索引  是没有办法驱动视图更新的
+      因为Vue不能检测到数组索引的变动
+      Vue.set(data数据,修改值的索引,修改的参数)
+      Vue要大写
+      Vue提供的一个全局的方法  用于向响应式对象中添加一个属性 并且确保这个新属性也是响应式的
+      Vue.set = this.$set
+      push() pop() shift() unshift() splice() sort() reverse()
+      Vue重写的方法  用来实现响应式更新   
+    */
+    var app = new Vue({
+      el: '#app',
+      data: {
+        arr: ['a','b','c','d'],
+        person: {
+          name:'zs',
+          age: 18
+        }
+      },
+      methods: {
+        change(){
+          // this.arr[0] = 'x'
+          // console.log(1)
+          // Vue.set(this.arr,0,'x')
+          // this.$set(this.arr,0,'x')
+          // this.person.name = 'lisi'
+          // Vue.set(this.person,'name','lisi')
+          this.arr.splice(0,1,'x')
+        }
+      }
+    })
+  </script>
+</body>
+
+</html>