我知道我的问题并不新鲜,但我在这里和互联网上找到的所有解决方案都不起作用:(或者,我正在做一些完全错误的事情。
我需要在 Dart 和 JS 之间创建通信,并且我很想使用事件,因为这个想法看起来很简洁。
所以,我尝试了这个教程:https://dart.academy/easy-dart-js-interopt/ https://dart.academy/easy-dart-js-interopt/
我的JS代码:
var customEvent = new CustomEvent("fromJavascriptToDart");
window.onload = document.dispatchEvent(customEvent);
document.addEventListener("fromJavascriptToDart", test, false);
function test() {
console.log("Listening");
}
正如我在控制台日志中看到的 Listening 一样,该事件已调度。
但当涉及到 dart 时,却没有任何作用。
我尝试了以下方法,但一切都失败了:
document.on['fromJavascriptToDart'].listen((CustomEvent event) {
print("HEY! I'M LISTENING!");
});
window.on["fromJavascriptToDart"].listen((e) => print( "HEY! I'M LISTENING!"));
window.on['foo'].add((e) => print(e.detail)); //This is not working, as there is no add method in Stream Event anymore
@Listen('fromJavascriptToDart')
void eventTest(){
print("HEY! I'M LISTENING!");
}
非常感谢任何帮助。
DartPad 示例 https://dartpad.dartlang.org/9cc2ecbe7a0b6479c56de882566982c9
document.on['fromJavascriptToDart'].listen((CustomEvent event) {
print("HEY! I'M LISTENING!");
});
工作正常。
-
@Listen()
是聚合物特定的
-
add
不存在(listen
应使用如上所示)
- 该事件似乎没有到达
window
,但我确信这在纯 JS 中表现相同。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)