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

编程入门 行业动态 更新时间:2024-10-16 18:35:08
本文介绍了如何在dart2js之后从JavaScript调用dart方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 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方法

本文发布于:2023-10-09 05:44:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   如何在   JavaScript   dart2js   dart

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!