如何在dart2js之后从JavaScript调用dart方法 [英] How to call a dart method from Javascript after dart2js

查看:2364
本文介绍了如何在dart2js之后从JavaScript调用dart方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;   

}


推荐答案

想要在Dart方法上创建一个Js函数绑定,你可以做几乎相同的事情:

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;
}

现在在Js端,您可以使用:

Now on Js side you can use :

getHelloOnTest();

这篇关于如何在dart2js之后从JavaScript调用dart方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆