0

I am learning vue3.

I want two sub-components to pass data.

index.js

import { createApp } from 'vue'

const bus = createApp({})
export default bus

App.vue

import bus from '@/bus/index'

mounted () {
    bus.$on('maizuo', (data) => {
      console.log('maizuo', data)
    })
}

Uncaught TypeError: _bus_index__WEBPACK_IMPORTED_MODULE_2__.default.$dispatch is not a function

Can someone help me?

Thank you.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565
dudulu
  • 754
  • 6
  • 17

1 Answers1

3

I saw from the official website that $on, $off and $once methods are removed from vue 3.

https://v3-migration.vuejs.org/breaking-changes/events-api.html#_2-x-syntax

Vue 3 requires a third-party library to achieve.

index.js

import mitt from 'mitt'

const bus = mitt()
export default bus

App.vue

mounted() {
    bus.on('maizuo', (data) => {
      console.log(data)
    })
  }
tony19
  • 125,647
  • 18
  • 229
  • 307
dudulu
  • 754
  • 6
  • 17