Vaadin 12 将对象传递给 JavaScript 的函数:无法编码 class
Vaadin 12 pass object to JavaScript's function: Can't encode class
Vaadin 12,Kotlin 项目
在我的 myPage.html
我有 javascript:
myObject.redirectToCheckout({
sessionId: "1111_2222",
}).
所以我需要从 Vaadin 12 调用 javaScript 函数 redirectToCheckout
并将正确的参数 作为对象 传递。
所以这是我的 Vaadin 片段:
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([=13=])", 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([=10=])", json)
Vaadin 12,Kotlin 项目
在我的 myPage.html
我有 javascript:
myObject.redirectToCheckout({
sessionId: "1111_2222",
}).
所以我需要从 Vaadin 12 调用 javaScript 函数 redirectToCheckout
并将正确的参数 作为对象 传递。
所以这是我的 Vaadin 片段:
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([=13=])", 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([=10=])", json)