GWT JsInterop 本机 class 问题

GWT JsInterop native class issue

我在处理一些 javascript 代码时遇到 JsInterop 问题。

JavaScript 类似于

com = { gwidgets: {} };
com.gwidgets.Spring = function () {
        this.name = "hello";    
};

com.gwidgets.Spring.prototype.getName = function () {return "test";
};

JsInterop class 是:

package com.gwidgets.leaflet;

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsType;

@JsType(isNative=true)
public class Spring {

    @JsMethod
    public native String getName();
}

但是,当我实例化 class 并尝试调用 getName() 方法时,出现错误:

leafletwrapper-0.js:1183 Uncaught TypeError: spring.getName is not a function

知道我的代码有什么问题吗?

根据您的java脚本,一种解决方案可能是将命名空间添加到注释

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsType;

@JsType(isNative=true, namespace = "com.gwidgets")
public class Spring {

    @JsMethod
    public native String getName();
}

或移动javaclassSpring打包com.gwidgets(与java脚本相同)

或更改 java 脚本的命名空间以匹配 class Spring

中的包