Vue父子组件声明周期
2018-09-21 11:31
197 查看
记录下,避免忘了,还要做
[code]<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue</title> <script src="./vue/vue.js"></script> <script src="./vue/vue-router.min.js"></script> <style> </style> </head> <body> <div id="app"> <span @click="childmsg = 'childnewsmsg'">{{ msg }}</span> <cpt-child :msg="childmsg"></cpt-child> </div> </body> <script> var child = Vue.extend({ template: '<div>cpt-child:{{ msg }}</div>', props: { msg: String }, beforeCreate: function(){ console.log('beforecreate parent') }, created: function(){ console.log('created parent') }, beforeMount: function(){ console.log('beforeMount parent') }, mounted: function(){ console.log('mounted parent') }, beforeUpdate: function(){ console.log('beforeUpdate parent') }, updated: function(){ console.log('updated parent') }, beforeDestroy: function(){ console.log('beforeDestroy parent') }, destroyed: function(){ console.log('destroyed parent') } }) var vm = new Vue({ el: '#app', data: { msg: 'msg', childmsg: 'childmsg' }, watch: { childmsg: function(newVal,oldVal){ console.log('watch change') } }, components: { 'cpt-child': child }, beforeCreate: function(){ console.log('beforecreate child') }, created: function(){ console.log('created child') }, beforeMount: function(){ console.log('beforeMount child') }, mounted: function(){ console.log('mounted child') }, beforeUpdate: function(){ console.log('beforeUpdate child') }, updated: function(){ console.log('updated child') }, beforeDestroy: function(){ console.log('beforeDestroy child') }, destroyed: function(){ console.log('destroyed child') } }) </script> </html>
这是按照渲染顺序console的结果,然后点击span后显示的结果。
结论:
先初始化父组件的数据,在初始化子组件的数据。
加载是相反的,先加载父组件,在加载子组件。
watch优先于update。
beforeUpdate是子组件优先,update是父组件优先。
总而言之,处理数据是子组件优先,加载到dom是父组件优先。
相关文章推荐
- vue 父子组件声明周期的执行顺序
- 基于VUE评论组件的组件自闭声明周期思考
- Vue父子组件传值
- vue父子组件的数据传递示例
- vue组件3-父子组件props传参
- vue开发:Vue 非父子组件通信方法(非Vuex)
- vue开发:vue父子组件与非父子组件之间的通信
- vue父子组件嵌套的时候遇到 - Component template should contain exactly one root element. If you are using v-i
- vue中的event bus非父子组件通信解析
- vue-父子组件间的相互传值与传址
- Vue中父子组件通讯之todolist组件功能开发
- VUE之父子组件间传值
- Vue项目首页_首页父子组件间的数据传递
- 总结vue父子组件之间的通信
- Vue 父子组件的数据传递、修改和更新
- vue中父子组件传递信息实现
- vue 组件父子孙 三代传递 以及 子传父 孙传父
- vue 自定义组件 v-model双向绑定、 父子组件同步通信的多种写法
- Vue-cli中非父子组件之间通信
- Vue2.0父子组件传递函数的教程详解