zsydgithub 11 月之前
父節點
當前提交
eb1a7554d0
共有 3 個文件被更改,包括 182 次插入0 次删除
  1. 49 0
      vue/7_computed.html
  2. 62 0
      vue/8_watch.html
  3. 71 0
      vue/9_生命周期.html

+ 49 - 0
vue/7_computed.html

@@ -0,0 +1,49 @@
+<!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">
+    {{msg}}
+    <br>
+    {{msg.split('')}}
+    <br>
+    方法: {{newMsg1()}}
+    <br>
+    计算属性: {{newMsg2}}
+
+  </div>
+  <script src="vue.js"></script>
+  <script>
+    var app = new Vue({
+      el: '#app',
+      data: {
+        msg: 'hello word'
+      },
+      methods: {
+        newMsg1(){
+          return this.msg.split('')
+        }
+      },
+      computed: {
+        /* 计算属性  依赖于其他的值去进行计算 并且缓存 */
+
+        /* 
+          在vue中 computed 比较特殊的属性  它的值是根据其他的值进行计算出来的结果
+          computed 可以使用get或者set的方法 来实现数据的读取和设置
+        */
+        newMsg2: function(){
+          return this.msg.split('')
+        }
+      }
+    })
+  </script>
+</body>
+
+</html>

+ 62 - 0
vue/8_watch.html

@@ -0,0 +1,62 @@
+<!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>
+    <h2>姓: {{firstName}}</h2>
+    <h2>名: {{lastName}}</h2>
+    <h2>姓名: {{fullName}}</h2>
+
+  </div>
+  <script src="vue.js"></script>
+  <script>
+    var app = new Vue({
+      el: "#app",
+      data: {
+        firstName: 'zhang',
+        lastName: 'san',
+        sex: '男'
+      },
+      methods: {
+        change() {
+          this.firstName = 'li'
+        }
+      },
+      computed: {
+        fullName: function () {
+          console.log(333)
+          return this.firstName + this.lastName
+        }
+      },
+      watch: {
+        firstName: function(){
+          console.log('fffff')
+        },
+        lastName: function(){
+          console.log('llllll')
+        }
+      }
+      /* 
+        在vue中 watch和computed都是用来观察数据变化的
+        作用不同
+
+
+        watch是响应式数据变化并且进行相应的操作
+        computed是用来计算一些基于响应式数据的操作
+
+
+        computed 是带有一定缓存的  当响应式数据没有变化的时候  computed 不会重新计算
+      */
+    })
+  </script>
+</body>
+
+</html>

+ 71 - 0
vue/9_生命周期.html

@@ -0,0 +1,71 @@
+<!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">
+    {{count}}
+    <button @click="add()">+1</button>
+    <button @click="des()">销毁</button>
+  </div>
+  <script src="vue.js"></script>
+  <script>
+    var app = new Vue({
+      el: '#app',
+      data: {
+        count: 1,
+        mount: setInterval(() => {
+          console.log(1)
+        }, 1000)
+        
+      },
+      methods: {
+        add(){
+          this.count++
+        },
+        des(){
+          clearInterval(this.mount)
+          this.$destroy()
+        }
+      },
+      //组件实例刚刚创建之前  属性计算之前
+      beforeCreate(){
+        console.log('beforeCreate',this.$data,this.$el)
+      },
+      //组件实例刚刚创建完成  属性绑定  dom没有生成   el不存在
+      created(){
+        console.log('created',this.$data,this.$el)
+      },
+      //挂载前
+      beforeMount(){
+        console.log('beforeMount',this.$data,this.$el)
+      },
+      //挂载后
+      mounted(){
+        console.log('mounted',this.$data,this.$el)
+      },
+      beforeUpdate(){
+        console.log('beforeUpdate',this.$data,this.$el)
+      },
+      updated(){
+        console.log('updated',this.$data,this.$el)
+      },
+      beforeDestroy(){
+        console.log('beforeDestroy',this.$data,this.$el)
+      },
+      destroyed(){
+        console.log('destroyed',this.$data,this.$el)
+      }
+
+
+    })
+  </script>
+</body>
+
+</html>