本文介绍了Vaadin 12将对象传递给JavaScript函数:无法编码类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Votin 12,Kotlin项目
Vaadin 12, Kotlin project
在我的myPage.html
中,我使用了javascript:
In my myPage.html
I has javascript:
myObject.redirectToCheckout({
sessionId: "1111_2222",
}).
因此,我需要从Vaadin 12中调用javaScript函数redirectToCheckout
并传递正确的参数作为对象.这是我的Vaadin代码段:
So I need to call javaScript function redirectToCheckout
from Vaadin 12 and pass correct param as object.So here my Vaadin snippet:
import com.vaadin.flow.component.dependency.HtmlImport
import com.vaadin.flow.component.dependency.JavaScript
import com.vaadin.flow.component.html.Div
import com.vaadin.flow.router.Route
import com.vaadin.flow.server.VaadinRequest
import java.io.Serializable
@Route(value = "redir")
@HtmlImport("styles/myPage.html")
class RedirectForm : Div() {
init {
val request = VaadinRequest.getCurrent()
val paramGoto = request.getParameter("goto")
val redirect = Redirect("$paramGoto")
UI.getCurrent().getPage().executeJavaScript("myObject.redirectToCheckout($0)", redirect) // **error here**
}
inner class Redirect : Serializable {
var sessionId: String
constructor(sessionId: String) {
this.sessionId = sessionId
}
}
}
但我收到错误消息:
Caused by: java.lang.IllegalArgumentException: Can't encode class com.myproject.view.RedirectForm$Redirect to json
at com.vaadin.flow.internal.JsonCodec.encodeWithoutTypeInfo(JsonCodec.java:165)
at com.vaadin.flow.internal.JsonCodec.encodeWithTypeInfo(JsonCodec.java:80)
at com.vaadin.flow.component.page.Page.executeJavaScript(Page.java:338)
at com.myproject.view.RedirectForm.<init>(RedirectView.kt:28)
... 50 common frames omitted
推荐答案
使用JsonObject
代替Redirect
:
val request = VaadinRequest.getCurrent()
val paramGoto = request.getParameter("goto")
val json = Json.createObject()
json.put("sessionId", "$paramGoto")
UI.getCurrent().getPage().executeJavaScript("myObject.redirectToCheckout($0)", json)
这篇关于Vaadin 12将对象传递给JavaScript函数:无法编码类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!