vaadin - Vaadin Flow:如何将类名添加到模板
问题描述
我在模板文件中有一个 Vaadin 组件:
import {LitElement, html} from 'lit-element';
import '@vaadin/vaadin-text-field/vaadin-text-field.js';
import '@vaadin/vaadin-button/vaadin-button.js';
class HelloWorld extends LitElement {
render() {
return html`
<div>
<vaadin-text-field id="firstInput"></vaadin-text-field>
<vaadin-button id="helloButton">Click me!</vaadin-button>
</div>`;
}
}
customElements.define('hello-world', HelloWorld);
和服务器端java类文件:
@Tag("hello-world")
@JsModule("./src/hello-world.ts")
public class HelloWorld extends LitTemplate {
/**
* Creates the hello world template.
*/
public HelloWorld() {
addClassName("my-style"); // Method not available!!
}
}
在 java 类中,我想为整个模板组件添加一个类名以动态设置它的样式。但是“addClassName()”方法在 LitTemplate 中不可用。是否可以将类名添加到 LitTemplate?在浏览器检查器中,我可以手动将 'class="my-style"' 添加到 hello-world 组件,它可以工作。
解决方案
使用HasStyle
mixin 访问该方法:
public class HelloWorld extends LitTemplate implements HasStyle { ... }
您可以查看实现以查看下面的内容https://github.com/vaadin/flow/blob/master/flow-server/src/main/java/com/vaadin/flow/component/HasStyle.java
推荐阅读
- elasticsearch - 如何从 Kibana 仪表板添加包含过滤器?
- c# - VS2019 Find 进入死循环
- javascript - 尝试导入项目时出现错误“对每个 HTML 规范的模块脚本强制执行严格的 MIME 类型检查”
- excel - 如何从 ms-excel 的另一列中删除相同的单词?
- amazon-web-services - 如何从 aws lambda 函数的标头中检索 x-api-key
- r - R中的正则表达式和str_detect()
- wordpress - 每当最初添加 Wordpress 小部件表单中的项目时,它们都会被隐藏,这可能是什么原因造成的?
- ios - MapBox Route Progress RouteStepProgress 变量大多返回 nil
- java - 导入javafx无法解决
- javascript - “502 bad gateway” - 从 AWS Lambda 提供给 Application Load Balancer 的 1MB 限制静态页面