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

相关推荐
笨笨鸟慢慢飞几秒前
Vue3后退不刷新,前进刷新
前端
LuckySusu3 分钟前
【vue篇】SSR 深度解析:服务端渲染的“利”与“弊”
前端·vue.js
LuckySusu3 分钟前
【vue篇】SPA 单页面应用:现代 Web 的革命与挑战
前端·vue.js
LuckySusu4 分钟前
【vue篇】Vue 初始化页面闪动(FOUC)问题终极解决方案
前端·vue.js
fruge6 分钟前
从 0 到 1 理解前端工程化:图表化解析核心逻辑
前端
LuckySusu7 分钟前
【vue篇】技术分析:Template 与 JSX 的本质区别与选型指南
前端·vue.js
BestStarLi12 分钟前
个人写码感悟:TailwindCSS不要忽视子选择器
前端
_大学牲13 分钟前
Flutter 之魂 GetX🔥(三)深入掌握依赖管理
前端·flutter
今天头发还在吗20 分钟前
【框架演进】Vue与React的跨越性变革:从Vue2到Vue3,从Class到Hooks
javascript·vue.js·react.js
渣哥28 分钟前
从 AOP 到代理:Spring 事务注解是如何生效的?
前端·javascript·面试