vue package $ on, $ emit, $ off

1. Subscribe $ on events

$ on (eventName, callback) Parameter 1: 2 Parameter Name Event: Event Function 
determines whether there is the name of the current event, if a key does not exist, create event name value 
value is a callback push the array into an array 

const eventList = {} ; 
const $ = ON (eventName, the callback) => { 
     IF {(EventList [eventName]!) 
         EventList [eventName] = []; 
     } 
     EventList [eventName] .push (the callback) 
}

2. $ emit trigger event

$ emit (eventName, [params] ) Parameter 1: Parameter Name Event 2: [parameters need to pass] 
Analyzing the name of the current event exists, if it exists through the array to obtain all the functions and performs. Params is then passed to the function as an argument to the 

const EventList = {}; 
const = $ EMIT (eventName, params) => { 
    IF (EventList [eventName]) { 
         the let ARR = EventList [eventName]; 
         arr.map ( (CB) => { 
             CB (the params) 
         }) 
    } 
}

Solution 3. $ off event tied

$ off (eventName, [callback] ) Parameter 1: Parameter Name Event 2: [Event Function] 
determines whether the name of the current event exists, if there continues to determine whether there is the second argument, if there is found corresponding to the index then function removes the array 
if there is no empty the entire array will 

const EventList = {}; 
const $ = OFF (eventName, the callback) => { 
    IF (EventList [eventName]) { 
          IF (the callback) { 
                 the let EventList index = [ eventName] .indexOf (the callback); 
                 EventList [eventName] .splice (index,. 1) 
           } 
    } {the else 
           EventList [eventName] .length = 0;   
    } 
} 

Export default = { 
     $ ON, 
     $ EMIT, 
     $ OFF 
 }

  

Guess you like

Origin www.cnblogs.com/ccyq/p/11288057.html