GWT JsInterop-在JavaScript中扩展Java接口

GWT JsInterop-在JavaScript中扩展Java接口,gwt,gwt-jsinterop,Gwt,Gwt Jsinterop,因此,我们正在尝试使用JsInterop,以便在GWT应用程序中,我们可以使用外部用JavaScript创建的一些模块 我们有一个具有某种契约的接口,一些必须由所有视图实现的方法 例如: package com.foo; import jsinterop.annotations.JsPackage; import jsinterop.annotations.JsType; @JsType(namespace = JsPackage.GLOBAL) public interface MyVi

因此,我们正在尝试使用JsInterop,以便在GWT应用程序中,我们可以使用外部用JavaScript创建的一些模块

我们有一个具有某种契约的接口,一些必须由所有视图实现的方法

例如:

package com.foo;

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

@JsType(namespace = JsPackage.GLOBAL) 
public interface MyView {

    void doSomething();
    void doSomethingElse();
    Element getElement();   
}
现在想象一下,在JS端,我们实现了该接口:

export class MyViewImplementation extends window.$wnd.MyView {

  constructor() {
    super();
  }

  doSometing() {
    //Implementation goes here
  }

  doSomethingElse() {
    //Implementation goes here
  }

  getElement() {
    var div = document.createElement("div");
    div.textContent = "External Component Test";
    return div;
  }
}
这行吗? 目前,我有一个错误,上面写着:
Class extends value undefined不是构造函数或null

根据您对我的评论的反馈,我将从您的接口中删除
@JsType
,并引入一个实现该接口的
@JsType(isNative=true)

public interface MyView {

    void doSomething();
    void doSomethingElse();
    Element getElement();   
}

public class JavaView implements MyView {
    // . . .
}

@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class JsView implements MyView {

    @Override
    public native void doSomething();

    @Override
    public native void doSomethingElse();

    @Override
    public native Element getElement();   
}
然后在JavaScript中:

export class JsView {
    // . . .
}

根据您对我的评论的反馈,我将从您的接口中删除
@JsType
,并引入一个实现该接口的
@JsType(isNative=true)

public interface MyView {

    void doSomething();
    void doSomethingElse();
    Element getElement();   
}

public class JavaView implements MyView {
    // . . .
}

@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class JsView implements MyView {

    @Override
    public native void doSomething();

    @Override
    public native void doSomethingElse();

    @Override
    public native Element getElement();   
}
然后在JavaScript中:

export class JsView {
    // . . .
}

我相信您希望使用
@JsType(isNative=true,namespace=JsPackage.GLOBAL)
。看看这里的“从外部JavaScript脚本导入类型”一节:如果我使用isNative=true,那么我的GWT应用程序将无法编译。原因是这个接口也是由我的Java视图实现的,所以不能用isNative来标记它……在JS中使用
extends
接口意味着什么?在JS中,除非我弄错了,否则没有真正的接口,但接口代表了必须遵循的约定,对吗?因此,要在JS中“实现接口”,您只需要声明这些方法,而不是实际导出任何东西,对吗?我相信您希望使用
@JsType(isNative=true,namespace=JsPackage.GLOBAL)
。看看这里的“从外部JavaScript脚本导入类型”一节:如果我使用isNative=true,那么我的GWT应用程序将无法编译。原因是这个接口也是由我的Java视图实现的,所以不能用isNative来标记它……在JS中使用
extends
接口意味着什么?在JS中,除非我弄错了,否则没有真正的接口,但接口代表了必须遵循的约定,对吗?因此,要在JS中“实现接口”,您只需要声明这些方法,而不是实际导出任何东西,对吗?谢谢!我们最终做了类似的事情,因此,创建了一个实现该接口的新类,并使用@JsType公开该类,而不是在实际的接口上这样做。谢谢!我们最终做了类似的事情,因此,创建了一个实现该接口的新类,并使用@JsType公开该类,而不是在实际接口上这样做。