<!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>Vue生命周期</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="root"></div>
<script>
const app = new Vue({
el: '#root',
template: '<div>{{text}}</div>',
data: {
text: 0
},
beforeCreate () {
console.log(this.$el, 'beforeCreate')
},
created () {
console.log(this.$el, 'created')
},
beforeMount () {
// mark,服务端渲染的时候不执行
console.log(this.$el, 'beforeMount')
},
// render (h) {
// // 和template的作用一样,所以两个不应该同时存在
// // 在beforeMount 和 mounted中间执行
// console.log('render')
// return h('div', {}, this.text)
// // throw new TypeError('render error')
// },
mounted () {
// 服务端渲染的时候不执行
console.log(this.$el, 'mounted')
},
beforeUpdate () {
// 数据更新前
console.log(this.$el, 'beforeUpdate')
},
updated () {
// 数据更新后
console.log(this.$el, 'updated')
},
activated () {
// 和组件相关
console.log(this.$el, 'activated')
},
deactivated () {
console.log(this.$el, 'deactivated')
},
beforeDestroy () {
// 销毁前
console.log(this.$el, 'beforeDestroy')
},
destroyed () {
// 销毁后
console.log(this.$el, 'destroyed')
},
renderError (h, err) {
// 本组件的错才会执行,不包括子组件
return h('div', {}, err.stack)
},
errorCaptured () {
// 子组件的错也可以捕获到,会向上冒泡,正式环境可以使用
}
});
setTimeout(() => {
app.text += 1;
}, 1000);
setTimeout(() => {
app.$destroy();
}, 2000);
</script>
</body>
</html>
vue 生命周期
原创
©著作权归作者所有:来自51CTO博客作者DuebassLei的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Vue—生命周期
生命周期图:每个Vue实例在被创建时都要经过一系列的初始化过程——例如,需要设置数
Vue—生命周期 Vue html .net