首页 > 解决方案 > 为什么 vuejs3 应用程序中的发射器不捕获事件?

问题描述

在我的 vuejs3/bootstrap 4.3.1 应用程序中使用 "mitt": "^2.1.0" 我有一个页面,其中包含项目和子组件,当操作员单击此子组件的按钮时触发事件 listingHeaderRightButtonClickedEvent:

  export default {

    setup () {
      function triggerListingHeaderRightButtonClickedEvent () {
        console.log('SOURCE triggerListingHeaderRightButtonClickedEvent parentComponentKey::')
        console.log(parentComponentKey)  // IN BROWSER'S CONSOLE I SEE THIS OUTPUT
        emitter.emit('listingHeaderRightButtonClickedEvent', { parentComponentKey: this.parentComponentKey })
      }

在父页面中,我尝试捕获此事件:

  const emitter = mitt()

  export default {
    setup () {
      emitter.on('listingHeaderRightButtonClickedEvent', params => {
        console.log('TARGET listingHeaderRightButtonClickedEvent params::')
        console.log(params) // IN BROWSER'S CONSOLE THIS EVENT IS NOT TRIGGERED
        if (params.parentComponentKey === 'category') {
          console.log('!!!!!loadCategories::')
          loadCategories()
        }
      })
      console.log('adminCategoriesListInit emitter::')
      console.log(emitter)

输出 emmiter 对象我看到:https ://imgur.com/a/SmHD2Gz 有什么问题?

谢谢!

标签: eventemittervuejs3

解决方案


推荐阅读