继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Vue学习总结

MMTTMM
关注TA
已关注
手记 303
粉丝 65
获赞 364

1、Vue核心思想

  • 数据驱动

  • 组件化

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

webp

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()


webp

来自官网


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

控制台输出:


webp

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


    webp

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

    webp


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


    webp

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


    webp

  • destroyed
    当调用vm.$destroy(),将会先后执行beforeDestry()和 destroyed()



作者:fenerchen
链接:https://www.jianshu.com/p/9fe157c8e6b8


打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP