我正在关注 GWT 文档 Coding Basics - JavaScript: JsInterop通过注释 @JsMethod
将 Java 类导出到 JavaScript。但是,Java 类不会转换为 JavaScript。
这是我的 Java 类:
package io.mincongh.client;
import jsinterop.annotations.JsMethod;
public class ExportedMethods {
@JsMethod
public static String sayHello(String name) {
return "Hello, " + name;
}
}
我的项目是通过 GWT Maven 插件 2.8.2 在 Maven 中构建的:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
<goal>generateAsync</goal>
</goals>
</execution>
</executions>
<configuration>
<runTarget>StockMarket.html</runTarget>
<modules>
<module>io.mincongh.StockMarket</module>
</modules>
</configuration>
</plugin>
当我在浏览器控制台中调用导出的方法时。那么方法没有定义:
io.mincongh.client.ExportedMethods.sayHello('world');
VM59:1 Uncaught ReferenceError: io is not defined at :1:1
最佳答案
来自规范JsInterop v1.0: Nextgen GWT/JavaScript Interoperability ,段落@JsType:
Note that exporting of Java Objects to JavaScript to be accessed by their namespace (e.g. this sample) requires
--generateJsInteropExports
flag.
因此您需要在 Maven GWT 插件中指定此标志:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
...
<configuration>
<generateJsInteropExports>true</generateJsInteropExports>
</configuration>
</plugin>
关于javascript - 无法通过 GWT 中的 JsInterop 将 Java 类导出到 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49214080/