Vaadin 12将对象传递给JavaScript的函数:不能编码类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vaadin 12将对象传递给JavaScript的函数:不能编码类相关的知识,希望对你有一定的参考价值。

要求12,Kotlin项目

在我的myPage.html我有javascript

 myObject.redirectToCheckout(
                    sessionId: "1111_2222",
                ).

所以我需要从Vaadin 12调用javaScript函数redirectToCheckout并将正确的param作为对象传递。所以这里是我的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($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的函数:不能编码类的主要内容,如果未能解决你的问题,请参考以下文章

如何将对象传递给 innerHTML (JavaScript) 中的函数?

如何将对象传递给 .handlebars 模板并在 javascript 代码块中使用它?

在javascript中,我已将对象传递给ejs文件,但它仍然说对象为空

如何按值将对象传递给Angular2 +组件

将对象传递给自定义选民?

将对象传递给 UICollectionViewCell