手记

vue父子组件生命周期执行顺序

执行顺序:beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount->子mounted->父mounted。

<!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>Document</title>
  <script src="./lib/vue-2.4.0.js"></script>
</head>

<body>
  <div id="app">
    <login></login>
  </div>

  <script>
    var login = {
      template: '<h1>{{childMsg}}}</h1>',
      data(){
        return {
          childMsg:'child'
        }
      },  
      beforeCreate: function () {
        debugger;
        console.log("子组件的beforeCreate")
      },
      created: function () {
        debugger
        console.log("子组件的created")
      },
      beforeMount: function () {
        debugger
        console.log("子组件的beforeMount")
      },
      mounted: function () {
        debugger
        console.log("子组件的mounted")
      }
    }

    var vm = new Vue({
      el: '#app',
      data: {
        fatherMsg: "father"
      },
      methods: {},
      components: {
        login
      },
      beforeCreate: function () {
        debugger
        console.log("父组件的beforeCreate")
      },
      created: function () {
        debugger
        console.log("父组件的created")
      },
      beforeMount: function () {
        debugger
        console.log("父组件的beforeMount")
      },
      mounted: function () {
        debugger
        console.log("父组件的mounted")
      },
    });
  </script>
</body>

</html>
0人推荐
随时随地看视频
慕课网APP