使用JavaScript中的Dart类 [英] Using Dart classes from JavaScript

查看:144
本文介绍了使用JavaScript中的Dart类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Dart类(foo.dart):

I have a Dart class (foo.dart):

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

在将foo.dart编译为JavaScript之后,喜欢能够使用Foo像这样:

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"

我的问题:


  • 这是否可以?

  • li>
  • 如果没有,是否有计划(如果有)可以实现?

dart:js库文档状态

The dart:js library documentation states:


这个库还没有使Dart对象可以从JavaScript使用,他们的方法和proeprties [sic]是不可访问的,虽然它允许Dart函数传递

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.

这个单词yet提供了一些希望,但我在其他地方找不到这个话题。

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

编辑:

我意识到可以使用dart2js从JavaScript调用Dart函数。然而,我想做的是有点不同。我希望能够从JavaScript访问Dart类的所有功能。

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.

推荐答案

由于树干和缩小这通常是不可能的。如果你有一个Dart应用程序( main()),那么你可以使一个Dart函数可以从JavaScript中调用(参见如何从Javascript中调用Dart函数?为例)。

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.

这是相关的项目 https://github.com/dart-lang/js-interop

这篇关于使用JavaScript中的Dart类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆