Vue.js lifecycle hooks

Posted coderwhytop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vue.js lifecycle hooks相关的知识,希望对你有一定的参考价值。

export default {
    data() {
      return {}
    },
    mounted() {
      console.log(\'mounted hook called\')
    },
    errorCaptured(err, vm, info) {
      console.log(\'error captured in component\', vm)
      console.error(err)
      console.log(\'error info:\', info)
    },
    activated() {
      console.log(\'cached component is being used again\')
    },
    deactivated() {
      console.log(\'component is being kept alive in cache for now\')
    }
}
// 所以vue生命周期lifecycle是11个

 

export const LIFECYCLE_HOOKS = [
\'beforeCreate\',
\'created\',
\'beforeMount\',
\'mounted\',
\'beforeUpdate\',
\'updated\',
\'beforeDestroy\',
\'destroyed\',
\'activated\',
\'deactivated\',
\'errorCaptured\'
]

 

// 调用顺序为

\'beforCreate\',

\'create\',

\'beforeMount\'

\'mounted\',

\'beforeUpdata\',

\'updataed\',

\'beforDistory\',

\'distoryed\',

\'activated\',

\'deactivated\',

\'errorCaptured\'

 // 关于vue.js中created()和activated()的个人理解

created():在创建vue对象时,当html渲染之前就触发;但是注意,全局vue.js不强制刷新或者重启时只创建一次,也就是说,created()只会触发一次;

activated():在vue对象存活的情况下,进入当前存在activated()函数的页面时,一进入页面就触发;可用于初始化页面数据等

以上是关于Vue.js lifecycle hooks的主要内容,如果未能解决你的问题,请参考以下文章

Vue.js 系列教程 3:Vue

[Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose

[MST] Loading Data from the Server using lifecycle hook

vue.js 源代码学习笔记 ----- keep-alives

React拓展 - setState - 路由组件懒加载 - Hooks - Fragment - Context - PureComponent - 插槽 - 错误边界 - 组件通信方式总结(代码片

vue生命周期(lifecycle)以及对nextTick的理解