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同步父子組件和異步父子組件的生命周期順序問題,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
vue金額格式化保留兩位小數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了vue金額格式化保留兩位小數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
bootstrap vue.js實(shí)現(xiàn)tab效果
這篇文章主要為大家詳細(xì)介紹了bootstrap vue.js實(shí)現(xiàn)tab效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
基于Vue2.0+ElementUI實(shí)現(xiàn)表格翻頁功能
Element UI 是一套采用 Vue 2.0 作為基礎(chǔ)框架實(shí)現(xiàn)的組件庫(kù),它面向企業(yè)級(jí)的后臺(tái)應(yīng)用,能夠幫助你快速地搭建網(wǎng)站,極大地減少研發(fā)的人力與時(shí)間成本。這篇文章主要介紹了Vue2.0+ElementUI實(shí)現(xiàn)表格翻頁功能,需要的朋友可以參考下2017-10-10
Vue實(shí)現(xiàn)調(diào)節(jié)窗口大小時(shí)觸發(fā)事件動(dòng)態(tài)調(diào)節(jié)更新組件尺寸的方法
今天小編就為大家分享一篇Vue實(shí)現(xiàn)調(diào)節(jié)窗口大小時(shí)觸發(fā)事件動(dòng)態(tài)調(diào)節(jié)更新組件尺寸的方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Element-ui中的Cascader級(jí)聯(lián)選擇器基礎(chǔ)用法
這篇文章主要為大家介紹了Element-ui中的Cascader級(jí)聯(lián)選擇器基礎(chǔ)用法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

