chrome | vue2.0 | description |
---|---|---|
---- | beforeCreate | 组件实例刚被创建,组件属性计算之前,如data属性 |
---- | created | 组件实例创建完成,属性已绑定,但DOM还为生成,$el还不存在 |
document.readyState === loading | ---- | 页面正在加载中 |
document.readyState === interactive | ---- | 页面解析完毕 |
DOMContentloaded | ---- | DOM构建完毕时触发 |
---- | beforeMounted | 模板编译/挂载之前 |
---- | mounted | 模板编译/挂载之后 |
document.readyState === complete | ---- | 文档和所有子资源已完成加载。状态表示 load 事件即将被触发 |
load 事件触发 | ---- | 页面所有资源都加载完毕之后 |
---- | beforeUpdate | 组件更新之前 |
---- | update | 组件更新之后 |
---- | activated | for keep-alive,组件被激活时调用 |
---- | deactivated | for keep-alive,组件被移除时调用 |
beforeunload 事件触发 | beforeDestory | 组件销毁前调用 |
unload 事件触发 | Destoryed | 组件销毁后调用 |
浏览器页面加载生命周期测试用例:
<!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>
</head>
<body>
<script>
function log (text) {console.log(text)};
var startDate = new Date();
log(Date.now() - startDate + 'initial state' + document.readyState);
document.addEventListener('DOMContentLoaded', () => log(Date.now() - startDate + 'DOMContentLoaded'));
document.addEventListener('readystatechange', () => log('document.readystate:', document.readyState));
window.addEventListtener('onload', () => log('window onloaded'));
<iframe onload='log("iframe loaded")'></iframe>
<img src='' alt='blank'><img>
</body>
</html>
复制代码
vue2.0组件生命周期测试用例:
<!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 type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
</head>
<body>
<div id="app">
<p>{{ message }}</p>
</div>
<script type='text/javascript'>
var app = new Vue({
el: '#app',
data() {
return { message: 'vue life cycle' }
},
methods: {
log: function (text) {
console.log("%c%s", "color:red", text)
}
},
beforeCreate: function () {
console.group('beforeCreate 创建前状态===============》');
console.log('el: ' + this.$el);
console.log(this.$el);
console.log('data: ' + this.$data);
console.log('message: ' + this.message);
console.log('methods: ' + this.$methods);
},
created: function () {
console.group('created 创建后状态===============》');
console.log('el: ' + this.$el);
console.log(this.$el);
console.log('data: ' + this.$data);
console.log('message: ' + this.message);
console.log('methods: ' + this.$methods);
},
beforeMount: function () {
console.group('beforeMount 模板加载前状态===============》');
console.log('el: ' + this.$el);
console.log(this.$el);
console.log('data: ' + this.$data);
console.log('message: ' + this.message);
console.log('methods: ' + this.$methods);
},
mounted() {
console.group('mounted 模板加载后状态===============》');
console.log('el: ' + this.$el);
console.log(this.$el);
console.log('data: ' + this.$data);
console.log('message: ' + this.message);
console.log('methods: ' + this.$methods);
},
beforeDestroy() {
console.group('beforeDestory 销毁前状态===============》');
console.log('el: ' + this.$el);
console.log(this.$el);
console.log('data: ' + this.$data);
console.log('message: ' + this.message);
console.log('methods: ' + this.$methods);
},
destroyed() {
console.group('destoryed 创建前状态===============》');
console.log('el: ' + this.$el);
console.log(this.$el);
console.log('data: ' + this.$data);
console.log('message: ' + this.message);
console.log('methods: ' + this.$methods);
}
})
</script>
</body>
</html>
复制代码