自己理解的生命周期以及对DOM的操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>理解生命周期</title>
</head>
<body>
<div id="app">
<input type="type" v-model="num">
<p id="p1">创建前--颜色的变化</p>
<p id="p11">创建后--颜色的变化</p>
<p id="p2">载入前--颜色的变化</p>
<p id="p21">载入后--颜色的变化</p>
<p id="p3">更新前--颜色的变化</p>
<p id="p31">更新后--颜色的变化</p>
<p id="p4">销毁前--颜色的变化</p>
<p id="p41">销毁后--颜色的变化</p>
<button @click="add">add</button>
<button @click="del">del</button >
</div>
</body>
<script src="../vue.js"></script>
<script charset="UTF-8" type="text/javascript">
new Vue({
el:"#app",
data:{
num:0
},
methods:{
add:function () {
this.num++;
},
del:function () {
this.num = 0;
}
},
beforeCreate:function () {
console.log("beforeCreate=》创建前");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p1").style.color="red";
},
created:function () {
console.log("created=》创建后");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p11").style.color ="red";
},
beforeMount:function () {
console.log("beforeMount=》载入前");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p2").style.color ="red";
},
mounted:function () {
console.log("mounted=》载入后");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p21").style.color ="red";
},
beforeUpdate:function () {
console.log("beforeUpdate=》更新前");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p3").style.color ="red";
},
updated:function () {
console.log("updated=》更新后");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p31").style.color ="red";
},
beforeDestroy:function () {
console.log("beforeDestroy=》销毁前");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p4").style.color ="red";
},
destroyed:function () {
console.log("destroyed=》销毁后");
console.log(this.num,"num");
console.log(this.$el,"el的值");
document.getElementById("p41").style.color ="red";
}
})
</script>
</html>
最初加载效果
当点击 add 按钮 加载效果