摘要:生命周期在實(shí)例初始化之后,數(shù)據(jù)觀測(cè)和事件配置之前被調(diào)用。點(diǎn)擊修改數(shù)據(jù)在中使用組件與在中使用組件是一樣的效果在實(shí)例創(chuàng)建完成后被立即調(diào)用。
有時(shí)候,我們需要在實(shí)例創(chuàng)建過(guò)程中進(jìn)行一些初始化的工作,以幫助我們完成項(xiàng)目中更復(fù)雜更豐富的需求開(kāi)發(fā),針對(duì)這樣的需求,Vue提供給我們一系列的鉤子函數(shù)。
在實(shí)例初始化之后,數(shù)據(jù)觀測(cè) (data observer) 和 event/watcher 事件配置之前被調(diào)用。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.js">script>
head>
<body>
<div id="app">
{{ name }}
<button @click="myClick">點(diǎn)擊修改數(shù)據(jù)button>
div>
<script>
new Vue({
el: "#app",
// 在template中使用組件與在body中使用組件是一樣的
// template: ` `,
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>
效果:
在實(shí)例創(chuàng)建完成后被立即調(diào)用。在這一步,實(shí)例已完成以下的配置:數(shù)據(jù)觀測(cè) (data observer),屬性和方法的運(yùn)算,watch/event 事件回調(diào)。然而,掛載階段還沒(méi)開(kāi)始,$el
?屬性目前不可見(jiàn)。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="static/vue.min.js">script>
head>
<body>
<div id="app">
{{ name }}
<button @click="myClick">點(diǎn)擊修改數(shù)據(jù)button>
div>
<script>
new Vue({
el: "#app",
// 在template中使用組件與在body中使用組件是一樣的
// template: ` `,
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>
在掛載開(kāi)始之前被調(diào)用:相關(guān)的?render
?函數(shù)首次被調(diào)用。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.js">script>
head>
<body>
<div id="app">
{{ name }}
<button @click="myClick">點(diǎn)擊修改數(shù)據(jù)button>
div>
<script>
new Vue({
el: "#app",
// 在template中使用組件與在body中使用組件是一樣的
// template: ` `,
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>
el
?被新創(chuàng)建的?vm.$el
?替換,并掛載到實(shí)例上去之后調(diào)用該鉤子。如果 root 實(shí)例掛載了一個(gè)文檔內(nèi)元素,當(dāng)?mounted
?被調(diào)用時(shí)?vm.$el
?也在文檔內(nèi)。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.js">script>
head>
<body>
<div id="app">
{{ name }}
<button @click="myClick">點(diǎn)擊修改數(shù)據(jù)button>
div>
<script>
new Vue({
el: "#app",
// 在template中使用組件與在body中使用組件是一樣的
// template: ` `,
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>
數(shù)據(jù)更新時(shí)調(diào)用,發(fā)生在虛擬 DOM 打補(bǔ)丁之前。這里適合在更新之前訪問(wèn)現(xiàn)有的 DOM,比如手動(dòng)移除已添加的事件監(jiān)聽(tīng)器。
該鉤子在服務(wù)器端渲染期間不被調(diào)用,因?yàn)橹挥谐醮武秩緯?huì)在服務(wù)端進(jìn)行.
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.js">script>
head>
<body>
<div id="app">
{{ name }}
<button @click="myClick">點(diǎn)擊修改數(shù)據(jù)button>
div>
<script>
new Vue({
el: "#app",
// 在template中使用組件與在body中使用組件是一樣的
// template: ` `,
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: ", this.$el);
console.log("data: ",
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/1537.html
摘要:之對(duì)基礎(chǔ)理解構(gòu)造器是一個(gè)構(gòu)造函數(shù),編程中稱之為構(gòu)造器每一個(gè)都是一個(gè)構(gòu)造函數(shù)的實(shí)例,這個(gè)過(guò)程叫做實(shí)例化構(gòu)造函數(shù)需要將其實(shí)例化后才會(huì)啟用構(gòu)造器要求實(shí)例化時(shí)需要傳入一個(gè)選項(xiàng)對(duì)象組件其實(shí)都是被擴(kuò)展的實(shí)例。 vue.js 之 對(duì)vue.js基礎(chǔ)理解 Vue構(gòu)造器 1 . Vue.js是一個(gè)構(gòu)造函數(shù),編程中稱之為構(gòu)造器 2 . 每一個(gè)new Vue() 都是一個(gè)Vue構(gòu)造函數(shù)的實(shí)例,這個(gè)過(guò)程叫...
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥(niǎo)雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:實(shí)例在文檔中經(jīng)常會(huì)使用這個(gè)變量名表示實(shí)例,在實(shí)例化時(shí),需要傳入一個(gè)選項(xiàng)對(duì)象,它可以包含數(shù)據(jù)模板掛載元素方法生命周期鉤子等選項(xiàng)。通俗說(shuō)就是實(shí)例從創(chuàng)建到銷毀的過(guò)程,就是生命周期。 Vue 實(shí)例中的生命周期鉤子 Vue 框架的入口就是 Vue 實(shí)例,其實(shí)就是框架中的 view model ,它包含頁(yè)面中的業(yè)務(wù)處理邏輯、數(shù)據(jù)模型等,它的生命周期中有多個(gè)事件鉤子,讓我們?cè)诳刂普麄€(gè)Vue實(shí)例的過(guò)程...
閱讀 1067·2021-11-23 09:51
閱讀 2412·2021-09-29 09:34
閱讀 3150·2019-08-30 14:20
閱讀 1045·2019-08-29 14:14
閱讀 3182·2019-08-29 13:46
閱讀 1077·2019-08-26 13:54
閱讀 1634·2019-08-26 13:32
閱讀 1427·2019-08-26 12:23