vue3.0生命周期的示例代碼
在組件化的框架中,比如Angular、React或Vue,都為組件定義了生命周期這個概念,每個組件實例在被創(chuàng)建時都要經(jīng)過一系列的初始化過程,例如:需要設(shè)置數(shù)據(jù)監(jiān)聽、編譯模板、將實例掛載到 DOM 并在數(shù)據(jù)變化時更新 DOM 等。同時,在這個過程中也會運行一些叫做生命周期鉤子的函數(shù),它們提供給用戶在組件的不同階段添加自己的代碼的機會。
使用過Vue2.x的朋友肯定對它的生命周期鉤子很熟悉了,因為在實際的開發(fā)過程中我們多多少少會用到他們,比如 created、mounted、destoryed等等。而在Vue3.0中,Vue2.x Options API形式的生命周期鉤子函數(shù)和新的Composition API都可以使用,來看個示例代碼就明白了:
const { onMounted } = Vue
const MyComp = {
// Options API
mounted() {
console.log('>>>>>> mounted 1')
},
setup() {
// Composition API
onMounted(() => {
console.log('++++++ mounted 2')
})
}
}
兩種形式的生命周期函數(shù)可以共存(當(dāng)然實際使用的時候最好只選用一種),它們都會被執(zhí)行。Composition API形式的生命周期函數(shù)都是在 setup 方法中被調(diào)用注冊。
最后,在實際的開發(fā)過程中,請注意一下Options API形式的組件生命周期鉤子和Composition API之間的實際對應(yīng)關(guān)系:
beforeCreate -> 請使用 setup()
created -> 請使用 setup()
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
errorCaptured -> onErrorCaptured
整體代碼如下:
const { createComponent, createApp, reactive } = Vue
// 計數(shù)器組件
const Counter = {
props: {
initCount: {
type: Number,
default: 0
}
},
template: `
<div class="counter-display">
<span class="counter-label">恭喜你,你已經(jīng)寫了</span>
<span class="counter-text">{{ state.count }}</span>
<span class="counter-label">斤代碼!</span>
</div>
<div class="counter-btns">
<button class="btn" @click="increase">寫一斤</button>
<button class="btn" @click="reset">刪庫啦</button>
</div>
`,
// 相當(dāng)于 vue2.x beforeCreated, created
setup(props) {
const countOps = useCount(props.initCount)
console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeCreated, created")
return { ...countOps }
},
onBeforeMount() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeMount")
},
onMounted() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 mounted")
},
onBeforeUpdate() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeUpdate")
},
onUpdated() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 updated")
},
onBeforeUnmount() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 beforeDestroy")
},
onUnmounted() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 destroyed")
},
onErrorCaptured() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 errorCaptured")
}
}
function useCount(initCount) {
const state = reactive({ count: initCount })
console.log("state reactive", state)
const increase = () => { state.count++ }
const reset = () => { state.count = 0 }
return { state, increase, reset }
}
// 創(chuàng)建一個 跟組件 app
const App = createComponent({
// 這個就相對于 在另一個 .vue 文件 引用 Counter 組件,需要在 components 屬性局部注冊組件
components: {
Counter,
},
// 掛載到 App 模板中
template: `
<div class="container">
<h3>計數(shù)器示例</h3>
<Counter />
</div>
`,
setup() {
console.log("App ===> 相當(dāng)于 vue2.x 中 beforeCreated, created")
},
onBeforeMount() {
console.log("App ===> 相當(dāng)于 vue2.x 中 beforeMount")
},
onMounted() {
console.log("App ===> 相當(dāng)于 vue2.x 中 mounted")
},
onBeforeUpdate() {
console.log("App ===> 相當(dāng)于 vue2.x 中 beforeUpdate")
},
onUpdated() {
console.log("App ===> 相當(dāng)于 vue2.x 中 updated")
},
onBeforeUnmount() {
console.log("App ===> 相當(dāng)于 vue2.x 中 beforeDestroy")
},
onUnmounted() {
console.log("App ===> 相當(dāng)于 vue2.x 中 destroyed")
},
onErrorCaptured() {
console.log("Counter ===> 相當(dāng)于 vue2.x 中 errorCaptured")
}
})
// 啟動
// container 就是相當(dāng)于 new Vue() 中 el 元素
const container = document.querySelector("#app")
// createApp() 就是相當(dāng)于 new Vue() 內(nèi)部返回的就是 new Vue()
const app = createApp()
// 掛載組件
app.mount(App, container)
轉(zhuǎn)載自:https://zhuanlan.zhihu.com/p/95968847
到此這篇關(guān)于vue3.0生命周期的示例代碼的文章就介紹到這了,更多相關(guān)vue3.0生命周期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 使用鼠標(biāo)滾動加載數(shù)據(jù)的例子
今天小編就為大家分享一篇vue 使用鼠標(biāo)滾動加載數(shù)據(jù)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue?ElementUI?table實現(xiàn)表格斜線分隔線
這篇文章主要為大家詳細(xì)介紹了Vue?ElementUI?table實現(xiàn)表格斜線分隔線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
vue2.0$nextTick監(jiān)聽數(shù)據(jù)渲染完成之后的回調(diào)函數(shù)方法
今天小編就為大家分享一篇vue2.0$nextTick監(jiān)聽數(shù)據(jù)渲染完成之后的回調(diào)函數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
基于Vue2的獨立構(gòu)建與運行時構(gòu)建的差別(詳解)
下面小編就為大家分享一篇基于Vue2的獨立構(gòu)建與運行時構(gòu)建的差別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
在vue中實現(xiàn)表單驗證碼與滑動驗證功能的代碼詳解
在Web應(yīng)用程序中,表單驗證碼和滑動驗證是常見的安全機制,用于防止惡意攻擊和機器人攻擊,本文將介紹如何使用Vue和vue-verify-code庫來實現(xiàn)表單驗證碼和滑動驗證功能,需要的朋友可以參考下2023-06-06
第一次使用webstrom簡單創(chuàng)建vue項目的一些報錯實戰(zhàn)記錄
在使用webstorm新建vue項目時常會遇到一些報錯,特別是新手第一次運行項目,這篇文章主要給大家介紹了關(guān)于第一次使用webstrom簡單創(chuàng)建vue項目的一些報錯實戰(zhàn)記錄,需要的朋友可以參考下2023-02-02

