我正在尝试使用 littemplate 从客户端调用服务器端函数。我有一个需要帮助的问题。我在服务器端添加了一个 HTML fromatted 文本,它有一个自定义组件“hello-world2”。
我在这个自定义组件中传递了一个属性(name="Vaadin")。我希望当用户单击“helloButton”时,应将值“Vaadin”发送到服务器并调用“acceptMessage”。但截至目前,我收到了附加屏幕截图中显示的错误。
我这样做是因为在我当前的应用程序中我已经生成了 HTML 表。对于几列我正在尝试合并和使用 <vaadin-button> HTML 标记。因此,当用户单击此按钮时,我希望服务器端出现消息/值以进行进一步处理。
请指导我如何做到这一点。
点亮模板
// hello-world2.ts
import { customElement, html, LitElement, property } from "lit-element";
import "@vaadin/vaadin-button/vaadin-button";
import "@vaadin/vaadin-text-field/vaadin-text-field";
import "@vaadin/vaadin-ordered-layout/vaadin-vertical-layout";
import { showNotification } from "@vaadin/flow-frontend/a-notification";
@customElement('hello-world2')
export class HelloWorld extends LitElement {
public $server1?: HelloWorldServerInterface;
@property({ type: String })
name = '';
render() {
return html`<vaadin-vertical-layout theme="padding spacing">
<vaadin-button id="helloButton" @click="${this.sendMessage}">Message Button</vaadin-button>
</vaadin-vertical-layout>`;
}
sendMessage() {
showNotification("Hello : " + this.name); //Works well, displays the notification
this.$server1!.acceptMessage(this.name); // Issue here.
}
}
interface HelloWorldServerInterface {
greet(): void;
acceptMessage(arg0: String): void;
}
爪哇
package com.example.application.views.helloworld;
import com.example.application.views.main.MainView;
import com.vaadin.flow.component.ClientCallable;
import com.vaadin.flow.component.Html;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.littemplate.LitTemplate;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.template.Id;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
@CssImport("./views/helloworld/hello-world-view.css")
@Route(value = "hello", layout = MainView.class)
@PageTitle("Hello World")
//@Tag("hello-world2") -- Commented, if I uncomment this then I can see two buttons on browser.
@JsModule("./views/littemplate/hello-world2.ts")
public class HelloWorldView extends HorizontalLayout {
public HelloWorldView() {
Html html = new Html("<hello-world2 name=\"Vaadin\"></hello-world2>");
add(html);
}
@ClientCallable
public void acceptMessage(String message) {
System.out.println("Message from client : " + message);
}
}