How to call a dart method from Javascript after dart2js

只谈情不闲聊 提交于 2019-12-01 08:50:28

问题


I got this Dart Script below and I want to access the methods from the class hello_world by JavaScript after I compiled the Dart Script with dart2js. Does anybody know how this works?! I already know how to access the functions like foo(...), thats not the problem, but it does not work the same way with classes and methods. And the tutorials on dartlang.org only explain how to access functions, not methods and classes. I dont get it...

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;   

}

回答1:


Assuming you want to create a Js function bind on a Dart method you can do almost the same thing :

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

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

Now on Js side you can use :

getHelloOnTest();


来源:https://stackoverflow.com/questions/22351766/how-to-call-a-dart-method-from-javascript-after-dart2js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!