【问题标题】:How to call a JS function with the new js (0.6.0) package如何使用新的 js (0.6.0) 包调用 JS 函数
【发布时间】:2016-01-14 23:12:25
【问题描述】:

index.html

<!doctype html>
<html>
  <head>
    <script>
      var testFunction = function() {return 'xxx'};
    </script>
  </head>
  <body>
    <script type="application/dart" src="index.dart"></script>
    <script src="packages/browser/dart.js"></script>
  </body>
</html>

index.dart

import 'dart:js' as js;
import 'package:js/js.dart';

@Js() // about to being changed to @JS
external String testFunction();

main() {
  // fails: Exception: Uncaught Error: No top-level method 'testFunction' declared.
  print(testFunction()); 

  // works
  print((js.context['testFunction'] as js.JsFunction).apply([]));
}

Dart VM 版本:“linux_x64”上的 1.13.0-edge.a598fea28cf26ed82b0a197e65af33a7edca5cac(2015 年 10 月 15 日星期四 18:02:15)

【问题讨论】:

    标签: dart dart-js-interop


    【解决方案1】:

    按预期工作。

    我刚刚在 Dart 更新期间运行了 Dartium,并且在我尝试示例时仍然运行旧版本。关闭并重新打开 Dartium 后,代码运行良好。

    【讨论】:

    • 请使用您问题上的编辑链接添加更多信息。 Post Answer 按钮应仅用于问题的完整答案。
    • 其实是一个完整的答案。问题中的代码运行良好,浏览器版本太旧,无法支持代码中使用的功能。我考虑结束这个问题,但决定反对它,因为我认为它可能会为其他人服务。我更新了我的答案。
    猜你喜欢
    • 2016-01-15
    • 2016-01-15
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多