Vue生命周期

组件生命周期--------组件从创建到销毁

创建 前 后

挂载 前 后

更新 前 后

销毁 前 后

vue给组件的某个阶段提供了特定的函数(钩子函数)来执行特定的逻辑,当到了某个节点会自动调用

创建前的函数 beforeCreate() { }

创建后的函数 created()

挂载前的函数 beforeMount()

挂载后的函数 mounted()

更新前的函数 beforeUpdate()

更新后的函数 updated()

销毁前的函数 beforeDestory()

销毁后的函数 destoryed()

组件中的data必须是一个函数,函数每次执行时候得到新对象

return 的是数据的对象

javascript 复制代码
export default {
    //组件名称方便调试
    name: 'HeaderComp' ,
    //组件data必须是一个函数,函数每次执行时候得到新对象
    //组件是可以复用,用对象形式导致组件的数据相互影响
    data ( ) {
        return {
          info: '123'
        }
},
    //创建前
beforeCreate ( ) {
    //拿不到数据
console.log ( ' beforeCreate( ) ', this)
	},
    //创建后
    created(){
        //发送网络请求
        console. log('created ( ) ', this.info)
	},
        //挂载前
  beforeMount(){
           console.log( ' beforeMount() ',this.$el)//null
 
        },
            //挂载后
     mounted(){
         //拿到渲染后的dom
               console.log ( '----' , document.getElementById( 'root ' ) )
          console.log( '  mounted() ',this.$el)
	},
    
    //数据更新前
    beforeUpdate(){
        console.log ( ' beforeUpdate( ) ' , this.info)
    },
        //数据更新后
     updated(){
            console.log ( ' updated( ) ', this. info)
     },
     //销毁前
     beforeDestory(){
         //资源清理---定时器取消、事件解绑等
       console.log ( ' beforeDestroy()' )  
         
},
     //销毁后
    destoryed(){
        console.log ( 'destroyed ()')
    },
    
        
         
}

页面一打开执行的生命周期:

beforeCreate->created->beforeMount->mounted

created 拿到数据

mounted 拿到dom元素

beforeDestory 数据清理

组件按用途分视图组件(配合路由使用)和业务组件

业务组件--------->components

视图组件------->views

相关推荐
RainbowSea1 分钟前
伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Boot 后端 + Vue3 - 05
vue.js·spring boot·后端
moyu844 分钟前
解密Vue组件中的`proxy`:此Proxy非彼Proxy
前端
用户84913717547168 分钟前
为什么大模型都离不开SSE?带你搞懂第1章〈SSE技术基础与原理〉
前端·网络协议·llm
随笔记11 分钟前
react中函数式组件和类组件有什么区别?新建的react项目用函数式组件还是类组件?
前端·react.js·typescript
在星空下14 分钟前
Fastapi-Vue3-Admin
前端·python·fastapi
FogLetter15 分钟前
面试官问我Function Call,我这样回答拿到了Offer!
前端·面试·openai
Juchecar15 分钟前
CSS布局模式详解 - 初学者完全指南
前端
emojiwoo17 分钟前
React 状态管理:useState 与 useDatePersistentState 深度对比
前端·javascript·react.js
cxyll123421 分钟前
从接口自动化测试框架设计到开发(三)主流程封装、返回数据写入excel
前端·python·excel