vue 生命周期

每个 Vue 实例在被创建之前都要经过一系列的初始化过程。例如需要设置数据监听、编译模板、挂载实例到 DOM、在数据变化时更新 DOM 等。同时在这个过程中也会运行一些叫做生命周期钩子的函数,给予用户机会在一些特定的场景下添加他们自己的代码。

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script type="text/javascript" src="../node_modules/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
     <p>{{ message }}
		<my-component></my-component>
     </p>
</div>
	<script type="text/javascript">
		var app = new Vue({
      		el: '#app',
      		data: {
          		message : "hello world!" 
      		},
       		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)  
        	},
        	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); //已被初始化
        	},
        	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); //已被初始化  
        	},
        	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)
        	},
        	components: {

    			'my-component': {
    				template: '<div>{{a}}</div>',
    				data: function(){
    					return {"a": "1"};
    				},
		    		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","a: " + this.a)  
		        	},
		        	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","a: " + this.a); //已被初始化
		        	},
		        	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","a: " + this.a); //已被初始化  
		        	},
		        	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","a: " + this.a); //已被初始化 
		        	},
		        	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","a: " + this.a); 
		        	},
		        	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","a: " + this.a); 
		        	},
		        	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","a: " + this.a); 
		        	},
		        	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","a: " + this.a)
		        	}
    			}
  			}	
    	});
	</script>
</body>
</html>

1、beforecreated:el 和 data 并未初始化
2、created:完成了 data 数据的初始化,el没有
3、beforeMount:完成了 el 和 data 初始化,模板未编译
  组件:
  4、beforecreated:el 和 data 并未初始化
  5、created:完成了 data 数据的初始化,el没有
  6、beforeMount:完成了 data 数据的初始化,el没有
  7、mounted :完成挂载 / 模板编译
8、mounted :完成挂载 / 模板编译

 

posted @ 2017-12-21 11:49  *﹏漸洐漸薳〆…  阅读(127)  评论(0)    收藏  举报