简体   繁体   English

Vue JS 2.0 子组件挂载回调

[英]Vue JS 2.0 Child Component Mounted Callback

I am making a form built of Vue JS components.我正在制作一个由 Vue JS 组件构建的表单。 I have the following components tree (each component contains the child below it ex. User Registration Form has the Form component as its direct child).我有以下组件树(每个组件都包含它下面的子组件,例如用户注册表单将 Form 组件作为其直接子组件)。

  1. User Registration Vue Component用户注册 Vue 组件
  2. Form Vue Component表单 Vue 组件
  3. Input Vue Component输入 Vue 组件
  4. Input Option Component输入选项组件

After all of the components have full rendered I need to run a function in the User Registration.在所有组件都完全呈现后,我需要在用户注册中运行一个函数。 I tried putting it in the mounted function in the User Registration Vue Component but it runs before the Option Components have completed their mounted function.我尝试将它放在用户注册 Vue 组件中的挂载功能中,但它在选项组件完成其挂载功能之前运行。

I read here: https://medium.com/@brockreece/vue-parent-and-child-lifecycle-hooks-5d6236bd561f that this should not happen and that the child component should be completely mounted before the running the parent mounted function, in this situation the User Registration Vue Component.我在这里读到: https ://medium.com/@brockreece/vue-parent-and-child-lifecycle-hooks-5d6236bd561f 这不应该发生并且子组件应该在运行父挂载函数之前完全挂载,在这种情况下,用户注册 Vue 组件。

I have about 100 components total including the Form, Inputs and all of the options.我总共有大约 100 个组件,包括表单、输入和所有选项。

I can only run this desired code in the User Registration Vue Component after everything has fully rendered and loaded.我只能在所有内容完全渲染和加载后,在用户注册 Vue 组件中运行所需的代码。 I tried using the jQuery(document).ready function but I have inconsistent results (sometimes the document is ready before the forms have fully mounted).我尝试使用 jQuery(document).ready 函数,但结果不一致(有时文档在表单完全安装之前就准备好了)。

Any recommendations?有什么建议吗?

If you need to know when a component has been created , mounted , updated etc. from a parent component, you can just define a listener using @hook: followed by the lifecycle hook name.如果您需要知道何时从父组件createdmountedupdated等组件,您可以使用@hook:后跟生命周期挂钩名称。

Eg from the parent component to execute doSomething() once the child component is mounted:例如,一旦子组件被挂载,从父组件执行doSomething()

<child-component @hook:mounted="doSomething" />
  1. Since the child mounting could be delayed more that nextTick(), you can emit an event programatically:由于子挂载可能比 nextTick() 延迟更多,因此您可以通过编程方式发出事件:

Child JS:儿童 JS:

{
    mounted: function() {
        this.$emit("mounted");
    }
}

Parent HTML:父 HTML:

<child @mounted="childMounted">

Parent JS:家长 JS:

{
    methods: {
        childMounted: function() {
            // TODO child action
        }
    }
}
  1. An alternative is to use the child component as a mixin另一种方法是将子组件用作 mixin

  2. Define a directive:定义一个指令:

     Vue.directive("mounted", { inserted: function(el, binding) { binding.value(); } });

You don't need to modify the child, only the parent:您不需要修改孩子,只需修改父母:

Parent HTML:父 HTML:

<child v-mounted="childMounted">

Parent JS same as in (2)父 JS 同(2)

mounted() hook of the parent component does not guarantee that all child component also finish rendering.父组件的mounted()钩子并不能保证所有子组件也完成渲染。

To execute something once the whole view has been rendered use vm.nextTick() inside the the parent component's mounted() hook.要在渲染整个视图后执行某些操作,请在父组件的mounted()挂钩中使用vm.nextTick()

//User Registration Vue Component

  mounted(){
    this.$nextTick(()=>{
      //execute your code
      console.log('Finished rendering the complete view')
    })
  }

Refer to - mounted lifecycle hook参考-mounted 生命周期钩子


Thanks @Tires, it works well谢谢@Tires,效果很好

Vue.directive("mounted", {
    inserted: function(el, binding) {
        binding.value();
    }
});

Parent:家长:

<child v-mounted="childMounted">

{
    methods: {
        childMounted: function() {
            // TODO child action
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM