简体   繁体   English

如何使用 ref<component> 在 vuejs 中?

[英]How to use ref on <component> in vuejs?

I am having two independent VUE components and I want to communicate between them.我有两个独立的 VUE 组件,我想在它们之间进行通信。

In the first component, I am using <component> and added ref to it.在第一个组件中,我使用了<component>并向其添加了 ref。

<component ref="dynamicComponent"></component>

and in that above dynamic component, there is a method(suppose dynamicMethod ) and I want to call that method from second component.在上面的动态组件中,有一个方法(假设dynamicMethod ),我想从第二个组件调用该方法。

So for that, I am using below code:为此,我使用以下代码:

this.$root.$refs.dynamicComponent.dynamicMethod();

But I am getting this.$root.$refs a blank object.但我得到this.$root.$refs一个空白对象。 How can I call that method.我怎么能调用那个方法。

Any help would be appreciated.任何帮助,将不胜感激。

Make sure you're trying to access $refs only after the component is mounted.确保您仅在安装组件后才尝试访问$refs You should be able to access the $refs in the mounted hook.您应该能够访问挂载钩子中的$refs

mounted: function() {
  console.log(this.$refs);
},

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

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