vue同步父子組件和異步父子組件的生命周期順序問題

關于vue組件的引入方式有兩種

牡丹網站制作公司哪家好,找創(chuàng)新互聯(lián)!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、成都響應式網站建設等網站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)成立與2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創(chuàng)新互聯(lián)。

一、 同步引入

例子: import Page from '@/components/page'

二、異步引入

例子:const Page = () => import('@/components/page')

或者: const Page = resolve => require(['@/components/page'], page)

兩種引入方式的不同之處在于:

同步引入時生命周期順序為:父組件的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>

vue同步父子組件和異步父子組件的生命周期順序問題

異步引入時生命周期順序:父組件的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'

結果:

vue同步父子組件和異步父子組件的生命周期順序問題

總結

以上所述是小編給大家介紹的vue同步父子組件和異步父子組件的生命周期順序問題,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!

網站欄目:vue同步父子組件和異步父子組件的生命周期順序問題
文章來源:http://muchs.cn/article38/jpgepp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供全網營銷推廣搜索引擎優(yōu)化、網站內鏈域名注冊、靜態(tài)網站、Google

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站建設