import( @/components/page ..."/>
您好,登錄后才能下訂單哦!
關(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ù)大家的!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。