简体   繁体   English

是否可以从JavaScript调用dartdevc生成的Dart代码?

[英]Is it possible to call Dart code generated with dartdevc from JavaScript?

I have a bunch of Dart libraries that a customer wants to call from JavaScript. 我有一堆客户想要从JavaScript调用的Dart库。 Is it possible to call functions created with dartdevc from JS? 是否可以从JS调用使用dartdevc创建的函数?

If you only want to call functions, you can probably do that in this way. 如果只想调用函数,则可以通过这种方式进行。 Although I only know it's works for functions, probably not for whole class. 尽管我只知道它适用于函数,但可能不适用于整个类。 I am also not sure about dartdevc support, but dart2js should be able to do this: 我也不确定dartdevc支持,但是dart2js应该能够做到这一点:

import 'dart:js' as js;

main() {
  js.context['methodFromDart'] = doMyStuff;
}

void doMyStuff(String text) => print(text);

And then in you Javascript you are free to do: 然后,您可以自由地使用Javascript:

methodFromDart("Hello world to Dart!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM