vue同步父子組件和異步父子組件的生命周期順序問題
關(guān)于vue組件的引入方式有兩種
一、 同步引入
例子: import Page from '@/components/page'
二、異步引入
例子:const Page = () => import('@/components/page')
或者: const Page = resolve => require(['@/components/page'], page)
兩種引入方式的不同之處在于:
同步引入時生命周期順序?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>

異步引入時生命周期順序:父組件的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同步父子組件和異步父子組件的生命周期順序問題,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
Vue 路由組件向app.vue主文件傳值的方式(兩種常見方式)
在Vue.js中,可以使用路由傳參的方式向App.vue主頁面?zhèn)鬟f數(shù)據(jù),有多種方法可以實(shí)現(xiàn)這一目標(biāo),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11
vue項(xiàng)目在env文件中設(shè)置的變量無效問題及解決
這篇文章主要介紹了vue項(xiàng)目在env文件中設(shè)置的變量無效問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
uni-app中App與webview雙向?qū)崟r通信詳細(xì)代碼示例
在移動應(yīng)用開發(fā)中,uni-app是一個非常流行的框架,它允許開發(fā)者使用一套代碼庫構(gòu)建多端應(yīng)用,包括H5、小程序、App等,這篇文章主要給大家介紹了關(guān)于uni-app中App與webview雙向?qū)崟r通信的相關(guān)資料,需要的朋友可以參考下2024-07-07
Electron進(jìn)程間通信的實(shí)現(xiàn)
本文主要介紹了Electron進(jìn)程間通信的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
解決獲取數(shù)據(jù)后this.$refs.xxx.toggleRowSelection無效的問題
這篇文章主要介紹了解決獲取數(shù)據(jù)后this.$refs.xxx.toggleRowSelection無效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
學(xué)習(xí)vue.js中class與style綁定
這篇文章主要和大家一起學(xué)習(xí)vue.js中class與style綁定操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
詳解vue 模擬后臺數(shù)據(jù)(加載本地json文件)調(diào)試
本篇文章主要介紹了詳解vue 模擬后臺數(shù)據(jù)(加載本地json文件)調(diào)試,具有一定的參考價值,有興趣的可以了解一下2017-08-08

