vue生命周期

最近项目中遇到了vue生命周期的问题

😂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Home.vue

<template>
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<HelloWorld msg="Welcome to Your Vue.js App"/>
<p>home</p>
</div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";
export default {
name: "home",
components: {
HelloWorld
},
data() {
return {};
},
beforeCreate() {
console.log("home beforeCreate");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
},
created() {
console.log("home created");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
},
beforeMount() {
console.log("home beforeMount");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
},
mounted() {
console.log("home mounted");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
},
beforeUpdate() {
console.log("home beforeUpdate");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
},
updated() {
console.log("home updated");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
},
beforeDestroy() {
console.log("home beforeDestroy");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
},
destroyed() {
console.log("home destroyed");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("");
}
};
</script>




Helloworld.vue
<template>
<div class="hello">
<h1>{{ msg }}</h1>
</div>
</template>

<script>
export default {
name: "HelloWorld",
props: {
msg: String
},
data() {
return {};
},
beforeCreate() {
console.log("helloworld beforeCreate");
console.log("el", this.$el);
console.log("data", this.$data);
console.log("获取不到msg的值");
console.log("");
},
created() {
console.log("helloworld created");
console.log("el", this.$el);
console.log("data", this.$data);
console.log(this.msg);
console.log("");
},
beforeMount() {
console.log("helloworld beforeMount");
console.log("el", this.$el);
console.log("data", this.$data);
console.log(this.msg);
console.log("");
},
mounted() {
console.log("helloworld mounted");
console.log("el", this.$el);
console.log("data", this.$data);
console.log(this.msg);
console.log("");
},
beforeUpdate() {
console.log("helloworld beforeUpdate");
console.log("el", this.$el);
console.log("data", this.$data);
console.log(this.msg);
console.log("");
},
updated() {
console.log("helloworld updated");
console.log("el", this.$el);
console.log("data", this.$data);
console.log(this.msg);
console.log("");
},
beforeDestroy() {
console.log("helloworld beforeDestroy");
console.log("el", this.$el);
console.log("data", this.$data);
console.log(this.msg);
console.log("");
},
destroyed() {
console.log("helloworld destroyed");
console.log("el", this.$el);
console.log("data", this.$data);
console.log(this.msg);
console.log("");
}
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
home beforeCreate
el undefined
data undefined

home created
el undefined
data {__ob__: Observer}

home beforeMount
el undefined
data {__ob__: Observer}

helloworld beforeCreate
el undefined
data undefined
获取不到msg的值

helloworld created
el undefined
data {__ob__: Observer}
Welcome to Your Vue.js App

helloworld beforeMount
el undefined
data {__ob__: Observer}
Welcome to Your Vue.js App

helloworld mounted
el <div data-v-469af010 class=​"hello">​…​</div>​
data {__ob__: Observer}
Welcome to Your Vue.js App

home mounted
el <div class=​"home">​…​</div>​
data {__ob__: Observer}



home beforeDestroy
el <div class=​"home">​…​</div>​
data {__ob__: Observer}

helloworld beforeDestroy
el <div data-v-469af010 class=​"hello">​…​</div>​
data {__ob__: Observer}
Welcome to Your Vue.js App

helloworld destroyed
el <div data-v-469af010 class=​"hello">​…​</div>​
data {__ob__: Observer}
Welcome to Your Vue.js App

home destroyed
el <div class=​"home">​…​</div>​
data {__ob__: Observer}
*/