English
emitEvent and onEvent
About 185 wordsLess than 1 minute
2025-09-22
emitEvent(eventName, option) and onEvent(eventName, handler) are used to implement communication inside the plugin.
These methods are used for internal plugin communication and must be used together.
emitEvent: method for sending eventsonEvent: method for listening to events
Parameters
emitEvent(eventName, option)
| Parameter | Description | Type |
|---|---|---|
| eventName | Event name | String |
| option | Custom parameters of the event | Object |
onEvent(eventName, handler)
| Parameter | Description | Type |
|---|---|---|
| eventName | Event name | String |
| handler | Event handler function | Function |
Return Value
None
Code Example
Inside the plugin:
module.exports = function (context, pluginService, pluginParam) {
return {
apply() {
return [
{
event: 'detail.render.after',
functional: function (pluginExecResult, options) {
console.log('custom plugin: detail.render.after exec')
// Send an event
context.emitEvent('detail-render-after', {test: 123})
}
}
]
}
}
}Inside the component:
Component({
lifetimes: {
attached() {
// Receive the event sent by the plugin
this.data.context.onEvent('detail-render-after', option => {
console.log(option) // {test: 123}
// do something
})
}
}
})Notes
- The
onEvent()method can only listen to events sent byemitEvent()within the lifecycle of the current detail page. - The event name listened to by
onEvent()must be the same as the event name sent byemitEvent().
