【问题标题】:Is there a better way to make a method of a Dart class callable from JS with the new js 0.6.0 package?有没有更好的方法可以使用新的 js 0.6.0 包从 JS 调用 Dart 类的方法?
【发布时间】:2016-01-15 10:39:59
【问题描述】:

index.html(头部)

<script>
  var callDartMethod = function(dartObject) {
    return dartObject.fullName();
  }
</script>

index.dart

import 'package:js/js.dart';

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

main() {
  final p = Person.create(firstName: 'Günter', lastName: 'Zöchbauer');
  print(callDartMethod(p)); // indirect call from JS
  // print(p.fullName()); // call from Dart directly
}

@Js() // about to being changed to @JS
class Person {
  external String get firstName;
  external set firstName(String firstName);

  external String get lastName;
  external set lastName(String lastName);

  external Function get fullName;
  external set fullName(Function function);

  external factory Person({String firstName, String lastName});

  static Person create({String firstName, String lastName}) =>
      new Person(firstName: firstName, lastName: lastName)
        // works but feels a bit cumbersome
        ..fullName = allowInteropCaptureThis(fullNameImpl);

  static String fullNameImpl(self) => '${self.firstName} ${self.lastName}';
}

【问题讨论】:

  • 嗨,冈特。我想知道您示例中的 fullNameJs getter 是否有误?应该是external Function get fullName; 吗?
  • 我想是的。似乎我中途改了名字,但不是到处改名。感谢您指出:)
  • 谢谢,例如,我认为它会帮助我。

标签: dart dart-js-interop


【解决方案1】:

简短回答:不。

pkg/js 目前专注于让 Dart 应用使用 JavaScript 库。

我们希望更轻松地将用 Dart 编写的 API 导出给 JavaScript 消费者,但这将在以后实现。

【讨论】:

    猜你喜欢
    • 2016-01-15
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2020-11-15
    • 2021-04-02
    • 1970-01-01
    • 2020-05-30
    • 2016-01-29
    相关资源
    最近更新 更多