Vue学习总结

1、Vue核心思想

  • 数据驱动
  • 组件化

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

Vue

 

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

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

  • Object.defineProperty()函数
    借助get和set函数
//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个生命周期函数。

  • beforeCreate()
  • created()
  • beforeMount()
  • mounted()
  • beforeUpdate()
  • updated()
  • beforeDestry()
  • destroyed()

 

来自官网


先看一段代码引用详解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>

控制台输出:

 

  • 在beforeCreate和created钩子函数之间的生命周期
    初始化数据。created()时,data属性及其内部数据已到位。

     

  • created钩子函数和beforeMount间的生命周期
    首先检测Vue实例中是否有el属性,本例中绑定的是’#app’,(没有就等待,直到有vm.$mount(el)被调用)如果有,那么继续查询实例中是否定义了template属性,如定义了模板属性,就把template编译到render函数中,否则就是用外部的html。
    beforeMount()函数执行时,dom中的占位符还没有被替换。

     

  • beforeMount和mounted 钩子函数间的生命周期
    替换占位符,dom中的数据完全展示出来。

     

  • Mounted
    dom元素和数据挂载绑定期间,如果更改数据,就会先后执行beforeupdate()和updated()

     

  • destroyed
    当调用vm.$destroy(),将会先后执行beforeDestry()和 destroyed()
    参考资料:
    vue官网
    详解vue生命周期



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值