简体   繁体   English

子组件渲染后的启动方法

[英]launch method after child component has been rendered

I'm using a v-if in my parent to conditional render a child component.我在父组件中使用v-if来有条件地呈现子组件。 Even when the child should not render the mounted function still gets executed and results in an error in console.即使孩子不应该渲染挂载的函数,它仍然会被执行并导致控制台出错。

How does one makes sure the child component is rendered when launching a method when the child component is rendered.当子组件被渲染时,如何确保在启动方法时子组件被渲染。

In my case I'm using an autofocus:就我而言,我使用的是自动对焦:

mounted: function () {
        // Autofocus input on load.
        this.$nextTick(() => this.$refs.input.focus());
    },

Error in nextTick: "TypeError: _this.$refs.input is undefined"

console.log(this.$refs.input) gives the object: console.log(this.$refs.input) 给出了对象:

<input class="form-control" data-v-661f7e55="" type="text" autocomplete="off">

Try this.尝试这个。

<input class="form-control" ref="input" type="text" autocomplete="off">

mounted(){
      this.$nextTick(() => this.$refs.input.focus())
    }

This is working fine for me.ref is used to register a reference to an element or a child component.这对我来说很好用。ref 用于注册对元素或子组件的引用。

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

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