vue生命周期函数

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>生命周期</title>
</head>
<body>
<div id="app">
<!--hello world-->
</div>
<script src="js/vue.js"></script>
<script>
//生命周期函数就是vue实例在某一个时间点会自动执行的函数
var vm = new Vue({
    el:'#app',
    template:"<div>{{test}}</div>",
    data:{
    test:"hello world"
    },
    beforeCreate:function() {
//当创建Vue实例的时候,当Vue实例进行基础的初始化之后就会自动调用这个函数
    console.log("beforeCreate");
    },
    created:function(){
    console.log("created");
    },
    //当函数进行处理外部注入和绑定相关内容时进行触发,vue的初始化都完成了之后,函数created会被执行.
//     是否存在el属性,是则会询问是否存在templetes模板属性,不存在则,则会吧el外层的html作为模板
beforeMount:function(){
console.log(this.$el);//el指最外层的元素即包括里面的内容
console.log("beforeMount");
},
// 在把文字渲染到模板之前执行beforeMount函数
mounted:function(){//当文字已经被渲染到页面 上的时候执行mounted函数
console.log(this.$el);
console.log("mounted");
},
//如何判断beforeMount和mounted是谁先执行呢
// 在beforeMount之前里输入console.log("this.$el");
beforeDestroy:function(){
console.log("beforeDestroy");
},
destroyed:function  () {
console.log("destroyed");
},
beforeUpdate:function(){  //数据发生改变还没有渲染之前,执行
console.log("beforeUpdate");
},
updated:function(){//重新渲染之后updated会被执行
console.log("updated");
}

})
//vue生命周期并不放在methods方法里
</script>

</body>

</html>



猜你喜欢

转载自blog.csdn.net/qq_41153478/article/details/80306090