Dart 是否支持使用现有的 JavaScript 库?

我了解 Dart 编译成 JavaScript,我阅读了关于 Library 的 飞镖语言规范,尽管我没有在那里看到答案。在他们的 讨论表格中搜索“现有”这个词也会出现3个不相关的结果。

有人知道 Dart 是否支持使用现有的 JavaScript 库,比如 jQuery 或 Raphael 吗?

15940 次浏览

You will not be able to call javascript directly from dart code. The native directive is reserved for the core libraries of dartc (dart:core, dart:dom, dart:html, dart:json, etc), which itself compiles to javascript.

The answer is now Yes! Dart now ships a JS-interop library to use existing JavaScript code with your Dart app. Learn more here: https://www.dartlang.org/articles/js-dart-interop/

There is also a dart:js library. And here is an article explaining how to use this library for interoperating with JavaScript.

There is now a new simpler way https://pub.dartlang.org/packages/js (currently version 0.6.0-beta.6)

Make JS classes and functions available to Dart like:

@JS("JSON.stringify")
external String stringify(obj);
@JS('google.maps')
library maps;


// Invokes the JavaScript getter `google.maps.map`.
external Map get map;


// `new Map` invokes JavaScript `new google.maps.Map(location)`
@JS()
class Map {
external Map(Location location);
external Location getLocation();
}


// `new Location(...)` invokes JavaScript `new google.maps.LatLng(...)`
//
// We recommend against using custom JavaScript names whenever
// possible. It is easier for users if the JavaScript names and Dart names
// are consistent.
@JS("LatLng")
class Location {
external Location(num lat, num lng);
}

for more see the readme of the package