在香草中订阅ionic 3事件Javascript
问题描述:
我想执行一些香草JavaScript代码(在外部文件中),当发布离子事件时。 但我不知道如何做到这一点。我想要这样的东西。在香草中订阅ionic 3事件Javascript
打字稿
this.event.publish('TestEvent',{data:123});
的JavaScript
document.addEventListener("TestEvent", function(data) {
alert('TestEvent');
});
答
我猜你可能只是再次触发事件上document
:
TS:
个this.event.publish('TestEvent', {data:123});
const event = new CustomEvent('TestEvent', {detail: {data: 123}});
document.dispatchEvent(event);
JS:
document.addEventListener('TestEvent', function (event) {
alert('TestEvent');
console.log(event.detail.data); // prints 123
});
更多关于MDN自定义事件:
https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
'的console.log(event.data);''显示undefined' – shah
抱歉,我的不好,会更新我的答案。您需要将有效载荷包装到另一个对象中... –