Web前端之路程序员让前端飞

Vue学习总结

2018-06-05  本文已影响65人  fenerchen

1、Vue核心思想

2、Vue通过MVVM的数据绑定实现自动同步

Vue

View就是DOM层,ViewModel就是通过new Vue()的实例对象,Model是原生js。当用户行为修改了DOM,ViewModel对修改的行为进行监听,监听到了后去更改Model层的数据,然后再通过ViewModel去改变View,从而达到自动同步。

3、Vue如何实现双向数据绑定

//html
<input type="text" name="" id="inp">
 <span id="sp"></span>

//js
 var obj = {};
    var inp = document.getElementById('inp');
//obj表示被操作的对象,‘username’表示添加的属性,get和set函数在username属性获取或者改变值时自动调用。
    Object.defineProperty(obj, 'username', {
        //控制台输入obj.username,自动调用get方法
        get: function() {
           console.log(this._username)
        },
        //控制台输入obj.username=0,自动调用set方法,set(0)
        set: function(val) {
            inp.value = val;
            document.getElementById('sp').innerText = val;
            this._username=val;
        }
    })
    inp.addEventListener('keyup', function(e) {
        obj.username = e.target.value;
    })

4、生命周期

vue的生命周期就是从创建到销毁的过程中,在某个特定的时间点自动执行的函数,一共有8个生命周期函数。

来自官网
先看一段代码引用详解vue生命周期
,在浏览器中查看输出:
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>vue生命周期学习</title>
      <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
      <!-- <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script> -->
    </head>
    <body>
      <div id="app">
        <h1>{{message}}</h1>
      </div>
    </body>
    <script>
      var vm = new Vue({
        el: '#app',
        data: {
          message: 'Vue的生命周期'
        },
        //数据都还没有
        beforeCreate: function() {
          console.group('------beforeCreate创建前状态------');
          console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
          console.log("%c%s", "color:red","data   : " + this.$data); //undefined 
          console.log("%c%s", "color:red","message: " + this.message) 
        },
        //data和内部的属性有值了,但是el还没有值
        created: function() {
          console.group('------created创建完毕状态------');
          console.log("%c%s", "color:red","el     : " + this.$el); //undefined
          console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化 
          console.log("%c%s", "color:red","message: " + this.message); //已被初始化
        },
        //el被初始化,但是内部的数据没有被替换
        beforeMount: function() {
          console.group('------beforeMount挂载前状态------');
          console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化
          console.log(this.$el);
          console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化  
          console.log("%c%s", "color:red","message: " + this.message); //已被初始化  
        },
        //替换el中的数据变量
        mounted: function() {
          console.group('------mounted 挂载结束状态------');
          console.log("%c%s", "color:red","el     : " + this.$el); //已被初始化
          console.log(this.$el);    
          console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
          console.log("%c%s", "color:red","message: " + this.message); //已被初始化 
        },
        beforeUpdate: function () {
          console.group('beforeUpdate 更新前状态===============》');
          console.log("%c%s", "color:red","el     : " + this.$el);
          console.log(this.$el);   
          console.log("%c%s", "color:red","data   : " + this.$data); 
          console.log("%c%s", "color:red","message: " + this.message); 
        },
        updated: function () {
          console.group('updated 更新完成状态===============》');
          console.log("%c%s", "color:red","el     : " + this.$el);
          console.log(this.$el); 
          console.log("%c%s", "color:red","data   : " + this.$data); 
          console.log("%c%s", "color:red","message: " + this.message); 
        },
        beforeDestroy: function () {
          console.group('beforeDestroy 销毁前状态===============》');
          console.log("%c%s", "color:red","el     : " + this.$el);
          console.log(this.$el);    
          console.log("%c%s", "color:red","data   : " + this.$data); 
          console.log("%c%s", "color:red","message: " + this.message); 
        },
        destroyed: function () {
          console.group('destroyed 销毁完成状态===============》');
          console.log("%c%s", "color:red","el     : " + this.$el);
          console.log(this.$el);  
          console.log("%c%s", "color:red","data   : " + this.$data); 
          console.log("%c%s", "color:red","message: " + this.message)
        }
      })
    </script>
    </html>

控制台输出:


上一篇 下一篇

猜你喜欢

热点阅读