我正在使用 Vue.js 2.0,我试图从“子组件”发出一个事件

2024-01-03

我正在使用 Vue.js 2.0,我试图从以下位置发出一个事件child component to parent component但它不起作用。

你可以在下面看到我的代码:

子组件:

<template>
  <button @click="confirmSendMessage">Send</button>
</template>

<script>
  export default {
    methods: {
      confirmSendMessage () {
        this.$emit('confirmed')
      }
    }
</script>

父组件:

<template>
    <ConfirmMessage/>
</template>

<script>
    import ConfirmMessage from './ConfirmMessage'
    export default {
        events: {
           confirmed() {
              console.log('confirmed')
           }
        },
        components: {
            ConfirmMessage
        }
   }
</script>

当我点击按钮时,Chrome 控制台上没有显示任何内容。 我不知道为什么。有谁能够帮助我?我是 Vue JS 的新手。


您错过了监听发出的事件。使用v-on https://v2.vuejs.org/v2/guide/events.html#Listening-to-Events收听事件:

<!--                               vv -> call method -->
<ConfirmMessage v-on:confirmed="confirmed" />
<!--                  ^^ -> emitted event -->
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我正在使用 Vue.js 2.0,我试图从“子组件”发出一个事件 的相关文章

随机推荐