标签:梳理 作用域 https 事件 结果 rop oct created clipboard
:
Vue 实例有一个完整的生命周期,也就是从开始创建、初始化数据、编译模板、挂载Dom→渲染、更新→渲染、卸载等一系列过程,我们称这是 Vue 的生命周期。通俗说就是 Vue 实例从创建到销毁的过程,就是生命周期。
destroyed :
当前组件已被删除,销毁监听事件 组件 事件 子实例也被销毁
这时组件已经没有了,你无法操作里面的任何东西了。
子父组件的生命周期
仅当子组件完成挂载后,父组件才会挂载
当子组件完成挂载后,父组件会主动执行一次beforeUpdate/updated钩子函数(仅首次)
父子组件在data变化中是分别监控的,但是在更新props中的数据是关联的(可实践)
销毁父组件时,先将子组件销毁后才会销毁父组件
兄弟组件的初始化(mounted之前)分开进行,挂载是从上到下依次进行
当没有数据关联时,兄弟组件之间的更新和销毁是互不关联的
mixin中的生命周期与引入该组件的生命周期是仅仅关联的,且mixin的生命周期优先执行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../vue.js"></script>
</head>
<body>
<div id="app">
<p>{{message}}</p>
<keep-alive>
<my-components msg="hello" v-if="show"></my-components>
</keep-alive>
</div>
</body>
<script>
var child = {
template: ‘<div>from child: {{msg}}</div>‘,
props: [‘msg‘],
data: function () {
return {
childMsg: ‘child1‘
};
},
deactivated: function () {
console.log(‘component deactivated!‘);
},
activated: function () {
console.log(‘component activated‘);
}
};
var app = new Vue({
el: ‘#app‘,
data: function () {
return {
message: ‘father‘,
show: true
};
},
beforeCreate: function () {
console.group(‘beforeCreate 创建前状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(state);
},
created: function () {
console.group(‘created 创建完毕状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(state);
},
beforeMount: function () {
console.group(‘beforeMount 挂载前状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(this.$el);
console.log(state);
},
mounted: function () {
console.group(‘mounted 挂载结束状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(this.$el);
console.log(state);
// this.message = ‘change‘;
},
beforeUpdate: function () {
console.group(‘beforeUpdate 更新前状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(this.$el);
console.log(state);
// this.message = ‘change2‘;
},
updated: function () {
console.group(‘updated 更新完成状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(this.$el);
console.log(state);
},
beforeDestroy: function () {
console.group(‘beforeDestroy 销毁前状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(this.$el);
console.log(state);
},
destroyed: function () {
console.group(‘destroyed 销毁完成状态===============》‘);
var state = {
‘el‘: this.$el,
‘data‘: this.$data,
‘message‘: this.message
}
console.log(this.$el);
console.log(state);
},
components: {
‘my-components‘: child
}
});
</script>
</html>
首先来梳理一下结构:
1.我们创建了一个Vue根实例命名为app,将其挂载到页面id为app的dom元素上。
2.局部注册的一个组件child并在根实例中将其注册使其可以在根实例的作用域中使用。
3.将子组件用<keep-alive> 包裹,为接下来的测试作准备。
在谷歌浏览器打开开发者工具,开始测试!
结果:注意区分下 beforeCreate() created() beforeMount() mounted()
页面渲染优先级:
render函数选项 > template选项 > outer HTML.
参考博客:https://segmentfault.com/a/1190000014640577
标签:梳理 作用域 https 事件 结果 rop oct created clipboard
原文地址:https://www.cnblogs.com/nayek/p/11787600.html