vue 生命周期钩子函数
2024-09-06 17:56:13
实例中的生命周期钩子可以分为以下8种情况: beforeCreate: 实例刚被创建,vue所有属性都还不存在 created: 实例创建完成,但$el还不存在 beforeMount:挂载之前 mounted:挂载之后,即data中的数值已经被渲染到元素中 beforeUpdate:更新之前 updated:更新之后 activated:组件被激活时 deactivated:组件移除时 beforeDestroy:实例被销毁前 destroyed:实例被销毁后
请看一个生命周期钩子Demo:
<html> <head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="../js/libs/vue/2.4.2/vue.js"></script>
</head> <body>
<div id="app">
<p>{{ message }}</p>
<button @click="updateMsg">updateMsg</button>
<button @click="destroy">destroy</button>
</div>
</body> <script>
var app = new Vue({
el: '#app',
data: {
message: "hello world",
currentView: 'dt1'
},
methods: {
updateMsg: function() {
this.message = "i be clicked";
},
destroy: function() {
app.$destroy();
}
},
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); //undefined
},
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 挂载前状态===============》(虚拟Dom技术,先把坑占住了。到后面mounted挂载的时候再把值渲染进去)');
console.log("%c%s", "color:red", "el : " + (this.$el)); //已被初始化,但实际上还没有渲染data中的数值到元素
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 更新前状态===============》(虚拟Dom技术,先把坑占住了。到后面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);
},
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);
},
activated: function() {
console.group('activated <keep-alive>组件被激活时状态===============》');
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);
},
deactivated: function() {
console.group('deactivated <keep-alive>组件被移除时状态===============》');
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 销毁完成状态===============》(销毁后vue失效,但元素还在)');
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)
}
})
</script> </html>
最新文章
- LogBack简易教程
- MySql 创建表 插入数据!
- jq制作好看的导航显示效果
- css背景图片拉伸 以及100% 满屏显示
- 【转】Could not write file XXX\.classpath解决
- Linux内核中常见内存分配函数(三)
- 更新Windows Azure Web Site中的Orchard版本
- LintCode-落单的数 III
- Git常用命令及软件推荐
- jqery autocomplete 动态传递参数的问题
- REST framework---基于类的视图
- Comet OJ 热身赛(E题)(处理+最短路算法)
- Setup Mission End
- 轻量级ORM工具Simple.Data
- OpenLayers中的球面墨卡托投影
- SpringKafka生产端配置类ProducerConfig.java源码
- jq实现 元素显示后 点击页面的任何位置除元素本身外 隐藏元素
- 删除Android自带的系统软件注意事项
- java通过CLASSPATH读取包内文件
- node js linux / OS 安装
热门文章
- LeetCode(83)Remove Duplicates from Sorted List
- 代码空间项目 -- 获取当前时间之前的某一天-Calender类的使用
- mysql错误指令:Failed to open file ";file_name"; error 2/error 22
- MySQL登陆及配置
- 网页上传FLV视频文件
- EnumDescription
- mongodb replica set 配置高性能多服务器详解
- kafka之二:Kafka 设计与原理详解
- MSSQL 调用 .net 代码
- <;正则吃饺子>; :关于前端往后端传递布尔值参数的问题