gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:02:54 25 4
gpt4 key购买 nike

我在下面得到了这个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com