简体   繁体   English

Gwt elemental2:如何在gwt JavaScript对象和JsInterop对象之间转换?

[英]Gwt elemental2: How can I convert between a gwt JavaScript object, and a JsInterop object?

Lets say I have a com.google.gwt.dom.client.Document gwtDocument node and I want to convert it to a elemental2.dom.Document ? 可以说我有一个com.google.gwt.dom.client.Document gwtDocument节点,我想将其转换为elemental2.dom.Document吗?

Since Document extends JavaScriptObject I assumed I could do something like: 由于Document扩展了JavaScriptObject,所以我假设我可以做类似的事情:

elemental2.dom.Document elementalDoc = (elemental2.dom.Document)(gwtDocument);

However the elemental2 classes using jsinterop don't extend JavaScriptObject. 但是,使用jsinterop的elemental2类不会扩展JavaScriptObject。 So how do I convert between the two? 那么如何在两者之间转换?

Thanks! 谢谢!

You can first cast to object, and then cast to the elemental type (1). 您可以先强制转换为对象,然后强制转换为元素类型(1)。 This is a bit ugly, so there is a utility lib that can be used in GWT and J2CL called jsinterop-base . 这有点丑陋,因此在GWT和J2CL中可以使用名为jsinterop-base的实用程序库。 The Js utility can be used to cast (2) and uncheckedCast (3) any object. 所述Js实用程序可用于cast (2)和uncheckedCast (3)的任何对象。 The uncheckedCast should be avoided and only used if you know what you are doing (ex. casting between iframes, or other special js situations). 应该避免使用uncheckedCast ,只有在知道自己在做什么时才使用它(例如,在iframe之间进行转换或其他特殊js情况)。

com.google.gwt.dom.client.Document gwtDocument = Document.get();
elemental2.dom.Document el1 = (elemental2.dom.Document) (Object) gwtDocument; //(1)
elemental2.dom.Document el2 = jsinterop.base.Js.cast(gwtDocument); //(2)
elemental2.dom.Document el3 = jsinterop.base.Js.uncheckedCast(gwtDocument); //(3)

So in client code, you should use Js.cast to cast GWT dom instances to elemental2 instances. 因此,在客户端代码中,应使用Js.castGWT dom实例转换为elemental2实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM