有時候,我們需要在實例創建過程中進行一些初始化的工作,以幫助我們完成項目中更複雜更豐富的需求開發,針對這樣的需求,Vue提供給我們一系列的鉤子函數。 vue生命周期 beforeCreate 在實例初始化之後,數據觀測 (data observer) 和 event/watcher 事件配置之前被調 ...
有時候,我們需要在實例創建過程中進行一些初始化的工作,以幫助我們完成項目中更複雜更豐富的需求開發,針對這樣的需求,Vue提供給我們一系列的鉤子函數。
vue生命周期
beforeCreate
在實例初始化之後,數據觀測 (data observer) 和 event/watcher 事件配置之前被調用。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.js"></script> </head> <body> <div id="app"> {{ name }} <button @click="myClick">點擊修改數據</button> </div> <script> new Vue({ el: "#app", // 在template中使用組件與在body中使用組件是一樣的 // template: `<cont></cont>`, data: { name: "Alex" }, methods: { init: function () { console.log(this.name); }, myClick: function () { this.name = "Pizza"; } }, beforeCreate() { console.group("beforeCreate"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, }); </script> </body> </html>
效果:
created
在實例創建完成後被立即調用。在這一步,實例已完成以下的配置:數據觀測 (data observer),屬性和方法的運算,watch/event 事件回調。然而,掛載階段還沒開始,$el
屬性目前不可見。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="static/vue.min.js"></script> </head> <body> <div id="app"> {{ name }} <button @click="myClick">點擊修改數據</button> </div> <script> new Vue({ el: "#app", // 在template中使用組件與在body中使用組件是一樣的 // template: `<cont></cont>`, data: { name: "Alex" }, methods: { init: function () { console.log(this.name); }, myClick: function () { this.name = "Pizza"; } }, beforeCreate() { console.group("beforeCreate"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, created() { console.group("Created"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); } }) </script> </body> </html>
beforeMount
在掛載開始之前被調用:相關的 render
函數首次被調用。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.js"></script> </head> <body> <div id="app"> {{ name }} <button @click="myClick">點擊修改數據</button> </div> <script> new Vue({ el: "#app", // 在template中使用組件與在body中使用組件是一樣的 // template: `<cont></cont>`, data: { name: "Alex" }, methods: { init: function () { console.log(this.name); }, myClick: function () { this.name = "Pizza"; } }, beforeCreate() { console.group("beforeCreate"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, created() { console.group("Created"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, beforeMount() { console.group("beforeMount"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); } }) </script> </body> </html>
mounted
el
被新創建的 vm.$el
替換,並掛載到實例上去之後調用該鉤子。如果 root 實例掛載了一個文檔內元素,當 mounted
被調用時 vm.$el
也在文檔內。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.js"></script> </head> <body> <div id="app"> {{ name }} <button @click="myClick">點擊修改數據</button> </div> <script> new Vue({ el: "#app", // 在template中使用組件與在body中使用組件是一樣的 // template: `<cont></cont>`, data: { name: "Alex" }, methods: { init: function () { console.log(this.name); }, myClick: function () { this.name = "Pizza"; } }, beforeCreate() { console.group("beforeCreate"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, created() { console.group("Created"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, beforeMount() { console.group("beforeMount"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, mounted() { console.group("mounted"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, }) </script> </body> </html>
beforeUpdate
數據更新時調用,發生在虛擬 DOM 打補丁之前。這裡適合在更新之前訪問現有的 DOM,比如手動移除已添加的事件監聽器。
該鉤子在伺服器端渲染期間不被調用,因為只有初次渲染會在服務端進行.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../statics/vue.js"></script> </head> <body> <div id="app"> {{ name }} <button @click="myClick">點擊修改數據</button> </div> <script> new Vue({ el: "#app", // 在template中使用組件與在body中使用組件是一樣的 // template: `<cont></cont>`, data: { name: "Alex" }, methods: { init: function () { console.log(this.name); }, myClick: function () { this.name = "Pizza"; } }, beforeCreate() { console.group("beforeCreate"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, created() { console.group("Created"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, beforeMount() { console.group("beforeMount"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, mounted() { console.group("mounted"); console.log("el: ", this.$el); console.log("data: ", this.$data); console.log("name: ", this.name); console.log("init: ", this.init); console.log("innerHTML: ", document.getElementById("app").innerHTML); }, beforeUpdate() { console.group("beforeUpdate"); console.log("el: "