Using Dart classes from JavaScript

谁都会走 提交于 2019-12-30 11:31:50

问题


I have a Dart class (foo.dart):

class Foo {
  void talk() {
    print('Hello');
  }
}

After compiling foo.dart to JavaScript, I'd like to be able to use Foo like this:

var foo = new Foo(); // from foo.dart.js
foo.talk() // prints "Hello"

My questions:

  • Is this currently possible?
  • If so, how?
  • If not, what plans, if any, are in place to make it possible?

The dart:js library documentation states:

This library does not yet make Dart objects usable from JavaScript, their methods and proeprties [sic] are not accessible, though it does allow Dart functions to be passed into and called from JavaScript.

That word "yet" offers some hope, but I've found very little on this topic anywhere else.

Edit:

I do realize it's possible to call Dart functions from JavaScript using dart2js. However, what I'm trying to do is somewhat different. I'd like to be able to access all of the functionality of a Dart class from JavaScript.


回答1:


Due to tree-shaking and minification this is normally not possible. If you have a Dart application (with a main() then you can make a Dart function available to be called from JavaScript (see How to call a Dart function from Javascript? for an example).

As far as I know there are plans to support your requirement but I have no idea about progress or when such a feature might be available.

This is the related project https://github.com/dart-lang/js-interop



来源:https://stackoverflow.com/questions/27321182/using-dart-classes-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!