@芥末的糖-----生命周期

2018-12-08  本文已影响0人  芥末的糖

以下代码直接运行,看console,就秒懂,别的就不BB了

<!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.js-lifecycle</title>
  <script src="../vue.js"></script>
  
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

</head>
<body>
  <div id="root">
    <div>{{ greeting }}</div>
  </div>
  <script>
    Vue.config.productionTip = false
    const vm = new Vue({
      el: '#root',
      data: {
        greeting: 'hello'
      },
      // 初始化时期
      beforeCreate () {
        console.log(this.$el)
        console.log(this.greeting)
      },
      created () {
        console.log(this.$el)
        console.log(this.greeting)
      },
      
      // 创建时期
      beforeMount () {
        console.log(this.$el.innerHTML)
        console.log(this.greeting)
      },
      mounted () {
        console.log(this.$el.innerHTML)
        console.log(this.greeting)
      },

      // 生存期
      beforeUpdate () {
        console.log('beforeUpdate:' + this.greeting)
      },
      updated () {
        console.log('updated:' + this.greeting)
      },

      // 销毁期
      beforeDestroy () {
        console.log('beforeDestroy:' + this.greeting)
      },
      destroyed () {
        console.log('destroyed:' + this.greeting)
      }
    })
  </script>
</body>
</html>
------------------------------------
undefined
undefined
undefined
hello
<div>{{ greeting }}</div>
hello
<div>hello</div>
hello

记得结合这张图!

z.png

如果再不懂就放弃吧!!!

上一篇下一篇

猜你喜欢

热点阅读