美文网首页
vue生命周期-更新流程

vue生命周期-更新流程

作者: tutututudou | 来源:发表于2022-07-05 17:15 被阅读0次
lifecycle.png

beforeUpdate

  • 当数据一旦更新,就会调用这个构造
    在挂载以后,数据已经更新,但是页面还没更新
更新.PNG
 <div class="root" :n="n">
    <h1>让n加1:   {{n}}</h1>
    <button @click="add">点击加1</button>
  </div>
  <script>
  Vue.config.devtools = true;
  const vm = new Vue({
    el:'.root',
    data:{
      n:1
    },
    methods:{
      add(){
        this.n++
      }
    },
    beforeCreate(){
      console.log('beforeCreate')
      console.log(this)
      // debugger
    },
    created(){
      console.log('created')
      console.log(this)
      //debugger
    },
    beforMount(){
      console.log('beforeMount')
      console.log(this)
      // debugger
    },
    mounted(){
      console.log('mounted')
      console.log(this)
      // debugger
    },
    beforeUpdate(){
      console.log('beforeUpdate')
      console.log(this.n)//点击button,n变成了2,但是页面没有有更新,数据更新了
      debugger
    }

  })
  // vm.$mount('.root')
  </script>
beforeUpdate-数据更新-页面没有更新.PNG
  • 页面数据未同步

updated

  • 根据新新旧虚拟DOM进行对比,最终呈现新DOM,完成Model>>>view的更新
新旧DOM对比.PNG
  <div class="root" :n="n">
    <h1>让n加1:   {{n}}</h1>
    <button @click="add">点击加1</button>
  </div>
  <script>
  Vue.config.devtools = true;
  const vm = new Vue({
    el:'.root',
    data:{
      n:1
    },
    methods:{
      add(){
        this.n++
      }
    },
    beforeCreate(){
      console.log('beforeCreate')
      console.log(this)
      // debugger
    },
    created(){
      console.log('created')
      console.log(this)
      //debugger
    },
    beforMount(){
      console.log('beforeMount')
      console.log(this)
      // debugger
    },
    mounted(){
      console.log('mounted')
      console.log(this)
      // debugger
    },
    beforeUpdate(){
      console.log('beforeUpdate')
      console.log(this.n)
      // debugger
    },
    updated(){
      console.log('updated')
      console.log(this.n)//新旧虚拟DOM对比,形成最终结果
      debugger
    }
  })
  // vm.$mount('.root')
  </script>
新旧虚拟DOM一致.PNG
  • 数据页面都是新的

debugger用于调试,因为页面一下子跑的太快了,要一步一步的看结果

相关文章

  • 《三》、Vue核心——生命周期

    Vue 实例生命周期 1、生命周期流程图 2、vue 生命周期分析 (1)、初始化显示;     ① before...

  • vue生命周期-更新流程

    beforeUpdate 当数据一旦更新,就会调用这个构造在挂载以后,数据已经更新,但是页面还没更新 页面数据未同...

  • vue3较vue2的特别之处 - 生命周期

    vue2 生命周期 vue3 生命周期 将 Vue2 的生命周期钩子代码更新到 Vue3 参考地址:https:/...

  • vue(2)生命周期

    一、Vue生命周期 vue 生命周期有 创建、挂载、更新、销毁 四个阶段 创建前(beforeCreate):实例...

  • vue.js 生命周期

    Vue实例 生命周期函数流程 —— beforeCreate、created、beforeMount、mounte...

  • react生命周期函数(老版本)

    图例说明 生命周期函数 生命周期: 组件的初始化流程: 组件的数据更新流程 例子演示

  • 小程序和vue生命周期对比学习

    先上图 生命周期流程对比 vue开始创建vue实例 (时间点)↓↓↓初始化事件和生命周期函数(时间段)—— 当其结...

  • vue生命周期理解

    vue生命周期流程图:4张图 生命周期的解析和应用: Vue 实例有一个完整的生命周期,也就是从开始创建、初始化数...

  • Vue生命周期

    Vue生命周期 {{name}} Vue把整个生命周期划分为创建、挂载、更新、销毁等阶段,每一个阶段都会给一些‘钩...

  • Vue 面试中常问知识点整理

    Vue的生命周期 生命周期:Vue实例从开始创建、初始化数据、编译模板、挂载Dom→渲染、更新→渲染、卸载等一系列...

网友评论

      本文标题:vue生命周期-更新流程

      本文链接:https://www.haomeiwen.com/subject/jyhjrrtx.html