有没有更好的方法来使用新的js 0.6.0包从JS调用Dart类的方法?

前端之家收集整理的这篇文章主要介绍了有没有更好的方法来使用新的js 0.6.0包从JS调用Dart类的方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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}';
}

解决方法

简答:不.

pkg / js目前专注于让Dart应用程序使用JavaScript库.

我们希望能够更轻松地将用Dart编写的API导出到JavaScript使用者,但这将在以后出现.

原文链接:https://www.f2er.com/js/157252.html

猜你在找的JavaScript相关文章