vue同步父子组件和异步父子组件的生命周期顺序问题

 更新时间:2018年10月07日 11:06:03   作者:不是laumlin  
这篇文章主要介绍了vue同步父子组件和异步父子组件的生命周期顺序问题,需要的朋友可以参考下

关于vue组件的引入方式有两种

一、 同步引入

例子: 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>

在这里插入图片描述

异步引入时生命周期顺序:父组件的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金额格式化保留两位小数的实现

    这篇文章主要介绍了vue金额格式化保留两位小数的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • bootstrap vue.js实现tab效果

    bootstrap vue.js实现tab效果

    这篇文章主要为大家详细介绍了bootstrap vue.js实现tab效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • 基于Vue2.0+ElementUI实现表格翻页功能

    基于Vue2.0+ElementUI实现表格翻页功能

    Element UI 是一套采用 Vue 2.0 作为基础框架实现的组件库,它面向企业级的后台应用,能够帮助你快速地搭建网站,极大地减少研发的人力与时间成本。这篇文章主要介绍了Vue2.0+ElementUI实现表格翻页功能,需要的朋友可以参考下
    2017-10-10
  • vue中echarts@4.9版本,地图的使用方式

    vue中echarts@4.9版本,地图的使用方式

    这篇文章主要介绍了vue中echarts@4.9版本地图的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • antd table按表格里的日期去排序操作

    antd table按表格里的日期去排序操作

    这篇文章主要介绍了antd table按表格里的日期去排序操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • Vue模板语法v-bind教程示例

    Vue模板语法v-bind教程示例

    这篇文章主要为大家介绍了Vue模板语法v-bind教程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Vue组件为什么data必须是一个函数

    Vue组件为什么data必须是一个函数

    这篇文章主要介绍了Vue组件为什么data必须是一个函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 浅谈Vue3的几个优势

    浅谈Vue3的几个优势

    这篇文章主要给大家分享的是Vue3的几个优势,Vue3仍然在源码、性能和语法 API 三个大的方面进行了优化,下面我们一起进入文章看看具体详情吧
    2021-10-10
  • Vue实现调节窗口大小时触发事件动态调节更新组件尺寸的方法

    Vue实现调节窗口大小时触发事件动态调节更新组件尺寸的方法

    今天小编就为大家分享一篇Vue实现调节窗口大小时触发事件动态调节更新组件尺寸的方法。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09
  • Element-ui中的Cascader级联选择器基础用法

    Element-ui中的Cascader级联选择器基础用法

    这篇文章主要为大家介绍了Element-ui中的Cascader级联选择器基础用法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06

最新评论