简体   繁体   English

无法在其他组件中定位方法

[英]Can't target method inside other component

How to target method inside other component?如何在其他组件中定位方法?

I'm working on a project where I want to target a method inside another component.我正在做一个项目,我想在另一个组件中定位一个方法。 I press a button inside the dashboard component and I want to change the data inside my line-chart component (which uses vue-chartjs ).我按下仪表板组件内的一个按钮,我想更改我的折线图组件(它使用vue-chartjs )内的数据。 How can I target this method?我该如何定位这种方法?

You can call children's method by referencing them through refs您可以通过refs引用孩子的方法来调用它们

In your example, your dashboard's template should looks like this :在您的示例中,仪表板的模板应如下所示:

<template>
  <div>
    <button @click="$refs.chart.yourMethod()">Call child method</button>
    <line-chart ref="chart"></line-chart>
  </div>
</template>

Well you could target it over $refs however, this is pretty dirty, as it prodvides a pretty strict binding of your componentes.好吧,您可以将其定位在$refs上,但这很脏,因为它为您的组件提供了非常严格的绑定。

A better solution would be to trigger an event (eventbus) or over an prop.更好的解决方案是触发事件(事件总线)或通过道具。

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

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