子组件挂载时,父组件是否将执行beforeUpdate / update挂钩函数?

时间:2019-01-28 06:05:15

标签: vue.js

在安装子组件时,父组件是否将执行beforeUpdate / update挂钩函数?

2 个答案:

答案 0 :(得分:0)

VueJS使用自下而上的方法,因此子组件首先运行。父组件最后收集所有子组件。

因此,如果孩子想听父母说的话,则可以通过on函数注册拳头,而父母可以通过emit函数注册拳头。

孩子也可以通过说$parent来设置父元素,但不建议这样做,因为其他孩子可以覆盖它。

答案 1 :(得分:0)

不,它不会执行。

beforeUpdate / update挂接在组件安装后起作用: https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram

父组件未安装而子组件未安装。

这里是一个示例: https://jsfiddle.net/2fg1vetx/

const Child = {
  template: '<span>i am "Child" component</span>',
  mounted() {
    console.log('Child is mounted');
  },
};

const Parent = {
  components: { Child },
  template: '<child />',
  beforeUpdate() {
    console.log('Parent beforeUpdate');
  },
  update() {
    console.log('Parent update');
  },
  mounted() {
    console.log('Parent is mounted');
  },
};

new Vue({
  el: '#app',
  components: { Parent },
});