dart - 如何在 dart2js 之后从 Javascript 调用 dart 方法

标签 dart dart-js-interop

我在下面得到了这个 Dart 脚本,我想在使用 dart2js 编译 Dart 脚本后通过 JavaScript 访问类 hello_world 中的方法。
有人知道这是如何工作的吗?!
我已经知道如何访问诸如 foo(...) 之类的函数,这不是问题,但它与类和方法的工作方式不同。
dartlang.org 上的教程只解释了如何访问函数,而不是方法和类。
我不明白...

import 'dart:js' as js;

class hello_world {

  String hello = 'Hello World!';

  String getHello() {
    print("getHello!!!!!");
    return hello;
  }

  void ausgabe() {
    print("Hallo Welt");
    //return 0;
  }
}

String foo(int n) {
  print("hallo");

  void foo2() {
    print("hallo2");
  }

  //works
  js.context['foo2'] = foo2;
  return 'Hallo';
}


 void main() {

  int zahl1 = 3;
  int zahl2 = 1234;
  String w = 'test';

  hello_world test = new hello_world();

  //works
  js.context['foo'] = foo;   

}

最佳答案

假设你想在 Dart 方法上创建一个 Js 函数绑定(bind),你几乎可以做同样的事情:

void main() {
  hello_world test = new hello_world();

  // define a 'getHelloOnTest' Js function
  js.context['getHelloOnTest'] = test.getHello;
}

现在在 Js 方面你可以使用:

getHelloOnTest();

关于dart - 如何在 dart2js 之后从 Javascript 调用 dart 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22351766/

相关文章:

dart - NetworkImage抖动- token 已过期

android - 如何解决webview抖动会覆盖已弃用的API?

dart - 将 Dart 2 Map 动态转换为 Javascript 对象

dart - 如何在 Dart js-interop 中为类 'Proxy' 定义方法?

dart - jQueryUI和jQueryUI触摸打洞不适用于Polymer.dart

flutter - 收集用户输入数据- flutter

dart - 装饰图像不被BoxDecoration剪切

flutter : any Solution to this Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'

dart - darjs 中的 for in 的替代方法是什么?

javascript - 如何在聚合物元素中使用来自 Dart 的 javascript