简体   繁体   English

如何将方法从一个组件传递到另一个组件并在 Vue.js 中使用它?

[英]How to pass a method from one component to another and use it onclick in Vue.js?

So in my App.vue component, I have two other components being rendered, and what I want to achieve is this, when the user clicks on the play button, animateRing() method from the component ProgressRing should be used, my code looks the following:所以在我的 App.vue 组件中,我还有另外两个组件正在渲染,我想要实现的是,当用户点击播放按钮时,应该使用组件 ProgressRing 中的 animateRing() 方法,我的代码看起来是下列的:

App.vue应用程序

<template>
  <div id="app">
    <ProgressRing>
        <PlayButton
          @click="animateRing(), active = !active"
          :class="{active: active}"
        />
    </ProgressRing>
  </div>
</template>

PlayButton.vue播放按钮.vue

<template>
  <button class="play-button" @click="$emit('click')">
    ...
    some irrelevant code here
    ...
  </button>
</template>

ProgressRing.vue进度环.vue

<template> 
...some code...
</template>

<script>
export default {
  methods: {
    animateRing(){
      console.log('I'm animated');
    }
  }
};
</script>

I can't seem to figure out the right way to do it.我似乎无法找出正确的方法来做到这一点。 What is the best way to make the method to work in the App component?使该方法在 App 组件中工作的最佳方法是什么? I'm relatively new to Vue and didn't really figure it out yet.我对 Vue 比较陌生,还没有真正弄明白。

Here's one way you can accomplish this:这是您可以实现此目的的一种方法:

App.vue应用程序

<template>
  <div id="app">
    <ProgressRing ref="progressRing">
        <PlayButton
          @click="animateRing(), active = !active"
          :class="{active: active}"
        />
    </ProgressRing>
  </div>
</template>

<script>
export default {
  methods: {
    animateRing(){
      this.$refs.progressRing.animateRing()
    }
  }
};
</script>

I'm curious about the reason for PlayButton being a child of ProgressRing , but not defined in ProgressRing.vue.我很好奇PlayButtonProgressRing的孩子,但没有在 ProgressRing.vue 中定义的原因。

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

相关问题 Vue.js:如何在方法中使用来自另一个组件的变量 - Vue.js: How to Use a Variable from Another Component in a Method 如何将数据(书籍数组长度)从一个组件传递到 vue.js 中的另一个组件? - How to pass data(books array length) from one component to another component in vue.js? Vue.js 如何将参数从一个组件传递到另一个组件? - Vue.js How to pass params from one component to another component? Vue.js:如何触发从一个组件到另一个组件的方法 - Vue.js : howto trigger method from one component to another 如何将数据从一个组件传递到另一个组件并将该数据存储到 vue.js 中的数组中? - How to pass data from one component to another component and store that data into an array in vue.js? 如何在 router.push() (Vue.js) 中将参数从一个组件传递到另一个组件? - How to pass parameters from one component to another in router.push() (Vue.js)? Vue.js - 如何从另一个组件调用方法 - Vue.js - How to call method from another component 如何将组件作为道具传递给Vue.js中的另一个组件 - How to pass component as prop to another component in Vue.js vue.js - 从另一个组件调用组件方法 - vue.js - call component method from another component Vue.js 显示从另一个组件调用一个组件 - Vue.js Display call one component from another component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM