Dart 是否支持使用现有的 JavaScript 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7716004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Will Dart support the use of existing JavaScript libraries?
提问by TMB
I understand Dart compiles to JavaScript, and I read the Dart Language Specon Libraries, although I didn't see an answer there. Also a search on their discussion formfor the word 'existing' turns up 3 results that are not related.
我了解 Dart 编译为 JavaScript,并且我阅读了关于库的Dart 语言规范,尽管我没有在那里看到答案。此外,在他们的讨论表上搜索“现有”一词会出现 3 个不相关的结果。
Does anyone know if Dart will support the use of existing JavaScript libraries such as jQuery or Raphael?
有人知道 Dart 是否支持使用现有的 JavaScript 库,例如 jQuery 或 Raphael?
采纳答案by Seth Ladd
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/
现在的答案是肯定的!Dart 现在提供了一个 JS-interop 库,以便在您的 Dart 应用程序中使用现有的 JavaScript 代码。在此处了解更多信息:https: //www.dartlang.org/articles/js-dart-interop/
回答by jtmcdole
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.
您将无法直接从 dart 代码调用 javascript。native 指令是为 dartc 的核心库(dart:core、dart:dom、dart:html、dart:json 等)保留的,它本身编译为 javascript。
回答by Günter Z?chbauer
There is now a new simpler way https://pub.dartlang.org/packages/js(currently version 0.6.0-beta.6)
现在有一种新的更简单的方法https://pub.dartlang.org/packages/js(当前版本 0.6.0-beta.6)
Make JS classes and functions available to Dart like:
使 JS 类和函数可用于 Dart,例如:
@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
有关更多信息,请参阅包的自述文件
回答by Shailen Tuli
There is also a dart:js
library. And here is an articleexplaining how to use this library for interoperating with JavaScript.
还有一个dart:js
图书馆。这里有一篇文章解释了如何使用这个库与 JavaScript 进行互操作。
回答by Lukas Renggli
See this thread from the discussion forum: Calling old javascript code.
请参阅讨论论坛中的此主题:调用旧的 javascript 代码。