vue同步父子組件和異步父子組件的生命周期順序問題
關(guān)于vue組件的引入方式有兩種
一、 同步引入
例子: import Page from '@/components/page'
二、異步引入
例子:const Page = () => import('@/components/page')
或者: const Page = resolve => require(['@/components/page'], page)
兩種引入方式的不同之處在于:
同步引入時(shí)生命周期順序?yàn)椋焊附M件的beforeMount、created、beforeMount --> 所有子組件的beforeMount、created、beforeMount --> 所有子組件的mounted --> 父組件的mounted
例子:
<! -- App.vue --> <template> <div id="app"> <New /> <!-- 子組件一 --> <Page /> <!-- 子組件二 --> <router-view/> </div> </template> <script> import Page from '@/components/page' // 同步方式引入 import New from '@/components/new' export default { name: 'App', components: { Page, New }, beforeCreate () { console.log('父組件App -------> App beforeCreate') }, created () { console.log('父組件App -------> App created') }, mounted () { console.log('父組件App -------> App mounted') }, beforeMount () { console.log('父組件App -------> App beforeMount') } } </script> </script> <!-- new.vue --> <template> <div> <p>this is a new component</p> </div> </template> <script> export default { name: 'new', created () { console.log('子組件new ----> new created') }, beforeCreate () { console.log('子組件new ----> new beforeCreate') }, mounted () { console.log('子組件new ----> new mounted') }, beforeMount () { console.log('子組件new ----> new beforeMount') } } </script> <!-- page.vue--> <template> <div> <Job /> <p>this is a page component</p> </div> </template> <script> import Job from '@/components/job' export default { name: 'page', components: { Job, }, beforeCreate () { console.log('子組件page ----> page beforeCreate') }, created () { console.log('子組件page ----> page created') }, mounted () { console.log('子組件page ----> page mounted') }, beforeMount () { console.log('子組件page ----> page beforeMount') } } </script> <!-- job.vue --> <template> <div> <p>this is a job component, in page.vue</p> </div> </template> <script> export default { name: 'job', created () { console.log('孫組件job ------> job created') }, beforeCreate () { console.log('孫組件job ------> job beforeCreate') }, mounted () { console.log('孫組件job ------> job mounted') }, beforeMount () { console.log('孫組件job ------> job beforeMount') } } </script>
異步引入時(shí)生命周期順序:父組件的beforeCreate、created、beforeMount、mounted --> 子組件的beforeCreate、created、beforeMount、mounted
在上面的代碼只需要修改引入的方式:
import Page from '@/components/page' // 同步方式引入 import New from '@/components/new' import Job from '@/components/job'
改為:
const Page = () => import ('@/components/page') // 異步引入 const New = () => import ('@components/new') const Job = () => import ('@/components/job'
結(jié)果:
總結(jié)
以上所述是小編給大家介紹的vue同步父子組件和異步父子組件的生命周期順序問題,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時(shí)回復(fù)大家的!
相關(guān)文章
vue動(dòng)態(tài)綁定ref(使用變量)以及獲取方式
這篇文章主要介紹了vue動(dòng)態(tài)綁定ref(使用變量)以及獲取方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08Vue 路由組件向app.vue主文件傳值的方式(兩種常見方式)
在Vue.js中,可以使用路由傳參的方式向App.vue主頁面?zhèn)鬟f數(shù)據(jù),有多種方法可以實(shí)現(xiàn)這一目標(biāo),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11vue項(xiàng)目在env文件中設(shè)置的變量無效問題及解決
這篇文章主要介紹了vue項(xiàng)目在env文件中設(shè)置的變量無效問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03uni-app中App與webview雙向?qū)崟r(shí)通信詳細(xì)代碼示例
在移動(dòng)應(yīng)用開發(fā)中,uni-app是一個(gè)非常流行的框架,它允許開發(fā)者使用一套代碼庫構(gòu)建多端應(yīng)用,包括H5、小程序、App等,這篇文章主要給大家介紹了關(guān)于uni-app中App與webview雙向?qū)崟r(shí)通信的相關(guān)資料,需要的朋友可以參考下2024-07-07Electron進(jìn)程間通信的實(shí)現(xiàn)
本文主要介紹了Electron進(jìn)程間通信的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05解決獲取數(shù)據(jù)后this.$refs.xxx.toggleRowSelection無效的問題
這篇文章主要介紹了解決獲取數(shù)據(jù)后this.$refs.xxx.toggleRowSelection無效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10學(xué)習(xí)vue.js中class與style綁定
這篇文章主要和大家一起學(xué)習(xí)vue.js中class與style綁定操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12詳解vue 模擬后臺數(shù)據(jù)(加載本地json文件)調(diào)試
本篇文章主要介紹了詳解vue 模擬后臺數(shù)據(jù)(加載本地json文件)調(diào)試,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08