How to access GWT's JsInterop exported types from browser console?

£可爱£侵袭症+ 提交于 2019-12-23 01:18:17

问题


I'm running a GWT application and I want to test something quickly with JsInterop.

Specifically, I exported an enum:

package com.mypackage.test

@JsType
enum MyEnum {
    A,
    B,
    C;
}

And I wanna check if I can access it properly before writing any code.

Docs show things like this:

var aClass = new com.gwt.example.MyClass('World');

But com is not defined in Window. So how can I access JsInterop from console to test things before writing code?


回答1:


Did you remember to pass the -generateJsInteropExports flag to the compiler (and to Super Dev Mode)? Without it, GWT will not export types, since that increases output size.

Also, that package doesn't look like the usual structure you expect to see in a GWT project (i.e. a client somewhere in there, corresponding to your GWT module) - did you remember to reference this package from your .gwt.xml file?

It is also possible that this should be public - the JsInterop code defaults to not showing non-visible members (since they aren't public, so aren't assumed to be consumed elsewhere). I'm not sure if this applies to types, or just members.


Finally, be aware that marking a Java enum with JsType doesn't magically make them make sense in JS - there will be a MyEnum type at the specified package, and each of those static members will be present, but they will be opaque JS objects, not strings or something else you can easily interact with, unless you also add other jsinterop annotations on the methods in that class. J2CL supports a @JsEnum which will turn them all into numeric constants automatically though, but this hasn't yet been ported to GWT2.



来源:https://stackoverflow.com/questions/54594322/how-to-access-gwts-jsinterop-exported-types-from-browser-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!