[英]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.我很好奇
PlayButton
是ProgressRing
的孩子,但没有在 ProgressRing.vue 中定义的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.