GWT编译报错:elemental2.dom类型无源码,是否遗漏模块继承?
问题描述
我尝试获取ID为calendarJS的div元素并传入构造函数,代码如下:
import elemental2.dom.Document; import elemental2.dom.Element; . . . Element calendarEl = new Document().getElementById("calendarJS");
页面加载后,GWT编译器出现如下错误:
[ERROR] Line 133: No source code is available for type elemental2.dom.Element; did you forget to inherit a required module? [ERROR] Line 133: No source code is available for type elemental2.dom.Document; did you forget to inherit a required module?
我已在pom.xml中引入elemental2-promise、elemental2-dom和elemental2-core依赖,请问还缺少什么配置?
解决方案
在GWT模块XML文件中添加Elemental2模块继承
GWT需要明确声明要使用的模块,即便已经在pom.xml中引入依赖,仍需在你的.gwt.xml配置文件里添加以下继承声明:<inherits name="elemental2.core.Core"/> <inherits name="elemental2.dom.Dom"/> <inherits name="elemental2.promise.Promise"/>这样GWT编译器才能识别Elemental2的类并编译对应源码。
检查依赖的scope配置
确保pom.xml中Elemental2相关依赖的scope未设置为provided或test,默认的compilescope即可(若手动修改过需改回),否则编译时GWT无法获取依赖的源码或类文件。示例依赖配置:<dependency> <groupId>com.google.elemental2</groupId> <artifactId>elemental2-dom</artifactId> <version>1.1.0</version> <!-- 使用你实际的版本号 --> </dependency> <dependency> <groupId>com.google.elemental2</groupId> <artifactId>elemental2-core</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>com.google.elemental2</groupId> <artifactId>elemental2-promise</artifactId> <version>1.1.0</version> </dependency>优化代码写法(可选)
获取Document实例无需new,使用Document.current()更符合Elemental2的规范:Element calendarEl = Document.current().getElementById("calendarJS");
内容的提问来源于stack exchange,提问作者Ricardo
相关产品推荐
相关产品推荐

