我正在使用Vue.js 2.0,并且尝试从“子组件"发出事件 [英] I am using Vue.js 2.0 and I am trying to emit an event from `child component`

查看:90
本文介绍了我正在使用Vue.js 2.0,并且尝试从“子组件"发出事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Vue.js 2.0,并且正在尝试从child componentparent component发出事件,但是它不起作用.

I am using Vue.js 2.0 and I am trying to emit an event from child component to parent component but it's not working.

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

You can see my code below:

子组件:

<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的新手.

When I click on the button, nothing appears to me on the Chrome console. I don't know why. Can anybody help me? I am new on Vue JS.

推荐答案

您错过了监听发出的事件的机会.使用 v-on 收听事件:

You missed to listen the emitted event. Use v-on to listen to the event:

<!--                               vv -> call method -->
<ConfirmMessage v-on:confirmed="confirmed" />
<!--                  ^^ -> emitted event -->

这篇关于我正在使用Vue.js 2.0,并且尝试从“子组件"发出事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆