$eventDispatch
如果在开发中要引用$eventDispatch服务,首先需要在config.json中的services中增加配置, 其次要在main.js或用户自己的创建的其他js文件中,引用该插件。
示例:
{
"develop":{
"html":"index.html",
"css":["./index.css"],
"entry":"./main.js",
"scripts":{
}
},
"plugins":[],
"libs":[],
"services":["$eventDispatch"],
"customAttributes":{
"pro":[
]
}
}
(function(){
return {
$data:{
properties:null
$eventDispatch:null
},
$methods:{
init(){
setTimeout(()=>{
this.$eventDispatch.notifyDataChanged("custom_event_name","hello world")
},2000)
}
},
$hookds:{
onInit(properties){
this.$eventDispatch=window.services.$eventDispatch;
this.$eventDispatch.subscribe('custom_event_name','_#visroot#_',(value)=>{
console.log(value)
})
this.init();
},
onDestroy(){
this.$eventDispatch.unsubscribe('custom_event_name','_#visroot#_')
},
setData(data,datasourceInfo){
if(this.chart&&data&&datasourceInfo&&data.data){
}
},
outSizeCallBack(info){
if(this.properties_){
$('#_#visroot#_main').css('height',this.properties_.style.height);
$('#_#visroot#_main').css('width',this.properties_.style.width);
}
}
}
}
})
$eventDispatch中负责事件的派送和订阅,该$eventDispatch 服务可以用来在多个自定义组件之间进行通讯。
订阅:subscribe(eventName,customId,callback)
参数:
eventName:自定义事件名称,需要与要触发的事件名称保持一致。
customId:自定义事件Id,如果同一个事件需要在不同位置同时注册侦听,这里需要明名称不一样的值。
callback:回调
派送:notifyDataChanged(eventName,value)
参数:
eventName:自定义事件名称,需要与要订阅的事件名称保持一致。
value:要传递的值
卸载:unsubscribe(eventName,customId)
参数:
eventName:自定义事件名称,需要与要订阅的事件名称保持一致。
customId:自定义事件Id,需要与注册的自定义Id保持一致