Vue2.0 Based on the communication tutorial between the Vue-CLI + WebPACK aspects (recommended)

We can then above Continue, this article we explain the communication of the brothers, the project structure is the same as above.

Create a file EventHandler.js in the src / assets directory. The role of the file is to pass the event between the information

EventHandler.js code:

  Import Vue from 'Vue'; Export Default New Vue (EXPORT DEFAULT NEW VUE)  
1, newly built a component Brother1.Vue

   
Z Country: ghostwu

Import EventHandler from '../assets/eventhandler. JS '; export default {data () {return {msg:' ''}}, methods: {send () {EventHandler. $ EMIT ('MyDefineev', "ghostw sang an atomic bomb to the island");}}, MOUNTED () {let _this = this; EventHandler. $ ON ("RDEfineev", Function (DATA) {_this.msg = data;});}}
. Send via EventHandler. $ EMITA custom event MYDEFINEEV
. Through MOTED [equivalent to jQuery’s Ready, native JS ONLOAD], this is the hook function of the Vue lifecycle, which is used to execute the code after the page is loaded, where is the RDEFINEEV event (Brother2.Vue defined)

2, in the Components directory, new component brother2.vue

R Country: eight
   Import EventHandler from '../assets/eventhandler.js'; Export Default {data () {return {msg: ''}} Methods: {defend () {EventHandler. $ EMIT ('RDEfineev', "Island Country adopts high-tech anti-primary bullet system");}}, mounted () {let _this = this; eventhandler. $ ON ("MyDefineev", Function (DATA) {_this.msg = data;});}} 

. Click the button to send the RDEfineev event
. When the document ready, the custom events departing from MyDefineev (Brother1.Vue) are received . Third, two peer components are called in App.Vue
[1

Import Brother1 from’./components/brother1.vue ‘; import brother2 from’ ./components/ Brother2.vue ‘; export default {components: {brother1, brother2}

Summary:
  Create a Event delivery center, such as EventHandler.js, using it as a mediation  
in need to pass the message, triggered a custom event, and transmit parameters The components that need to receive data are listened to the custom events in EventHandler. $ ON, and process the passing parameters during the callback function , this Vue2.0 is based on the Vue-CLI + WebPACK. Communication tutorial (recommended) is the full content of Xiaobian sharing to everyone, I hope to give you a reference, I hope everyone will support Tumi Cloud.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment