如果在相应的Java代码中引用对象

发布于 2025-02-12 12:22:35 字数 2387 浏览 3 评论 0原文

如果人们使用vaadin Designer(在Vaadin 14中)创建垂直布局类,并将其宽度设置为某个固定值(例如3%),一个人在相应的Java代码中对该垂直布局组件有一个引用,Vaadin将忽略宽度设置。奇怪的是,如果一个错误在Java代码中不引用垂直布局类,则此错误会消失。这是一个可说明错误的工作测试用例。目前,我的“黑客”是为了避免在我的Java代码中引用对象,或者如果必须,我认为我需要在Java代码本身中明确重置宽度逻辑。有什么想法吗?

import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import '@vaadin/vaadin-ordered-layout/src/vaadin-horizontal-layout.js';

class UiTestview extends PolymerElement {

    static get template() {
        return html`
<style include="shared-styles">
                :host {
                    display: block;
                    height: 100%;
                }
            </style>
<vaadin-horizontal-layout class="content" style="width: 100%; height: 100%;">
 <vaadin-vertical-layout id="left" style="width: 3%;background-color:blue">
 <label>left label</label>
 </vaadin-vertical-layout>
 <label style="flex-grow: 1;background-color:red">Label</label>
</vaadin-horizontal-layout>
`;
    }

    static get is() {
        return 'ui-testview';
    }

    static get properties() {
        return {
            // Declare your properties here.
        };
    }
}

customElements.define(UiTestview.is, UiTestview);

以及相应的Java代码:

package org.vaadin;

import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.template.Id;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.templatemodel.TemplateModel;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.polymertemplate.PolymerTemplate;


/**
 * A Designer generated component for the ui-testview template.
 *
 * Designer will add and remove fields with @Id mappings but
 * does not overwrite or otherwise change this file.
 */
@Route("testview")
@Tag("ui-testview")
@JsModule("./src/ui-testview.js")
public class UiTestview extends PolymerTemplate<UiTestview.UiTestviewModel> {


    @Id("left")
    private VerticalLayout left;

    /**
     * Creates a new UiTestview.
     */
    public UiTestview() {

    }

    /**
     * This model binds properties between UiTestview and ui-testview
     */
    public interface UiTestviewModel extends TemplateModel {
        // Add setters and getters for template properties here.
    }
}

If one creates a vertical layout class using Vaadin Designer (in Vaadin 14) and sets its width to some fixed value (eg 3%) and one has a reference to that vertical layout component in the corresponding java code, Vaadin will IGNORE the width setting. Curiously, this bug dissappears if one does NOT reference the vertical layout class in the java code. Here's a working test case to illustrate the bug. For now, my "hack" has been to avoid referencing the object in my java code, or if I must, then I think I need to explicitly reset the width logic in the java code itself. Any thoughts?

import {html, PolymerElement} from '@polymer/polymer/polymer-element.js';
import '@vaadin/vaadin-ordered-layout/src/vaadin-horizontal-layout.js';

class UiTestview extends PolymerElement {

    static get template() {
        return html`
<style include="shared-styles">
                :host {
                    display: block;
                    height: 100%;
                }
            </style>
<vaadin-horizontal-layout class="content" style="width: 100%; height: 100%;">
 <vaadin-vertical-layout id="left" style="width: 3%;background-color:blue">
 <label>left label</label>
 </vaadin-vertical-layout>
 <label style="flex-grow: 1;background-color:red">Label</label>
</vaadin-horizontal-layout>
`;
    }

    static get is() {
        return 'ui-testview';
    }

    static get properties() {
        return {
            // Declare your properties here.
        };
    }
}

customElements.define(UiTestview.is, UiTestview);

and the corresponding java code:

package org.vaadin;

import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.template.Id;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.templatemodel.TemplateModel;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.polymertemplate.PolymerTemplate;


/**
 * A Designer generated component for the ui-testview template.
 *
 * Designer will add and remove fields with @Id mappings but
 * does not overwrite or otherwise change this file.
 */
@Route("testview")
@Tag("ui-testview")
@JsModule("./src/ui-testview.js")
public class UiTestview extends PolymerTemplate<UiTestview.UiTestviewModel> {


    @Id("left")
    private VerticalLayout left;

    /**
     * Creates a new UiTestview.
     */
    public UiTestview() {

    }

    /**
     * This model binds properties between UiTestview and ui-testview
     */
    public interface UiTestviewModel extends TemplateModel {
        // Add setters and getters for template properties here.
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2025-02-19 12:22:35

基本上,Vaadin的Java侧(至少在Vaadin 14中)并不了解模板的任何初始客户端状态。这就是为什么Java-Getters通常不会返回模板中设置的值的原因。此外,verticallayout sets setWidth(“ 100%”) ,一旦将模板映射到模板,它实际上可能会覆盖模板中的宽度集。这是Vaadin 18中的改进。我怀疑这会被退回到Vaadin 14。

Basically the Java-side of Vaadin (at least in Vaadin 14) does not know about any initial client-side state from the template. That's why Java-getters will often not return values set in your template. Furthermore, VerticalLayout sets setWidth("100%") in its constructor which may actually override a width set in your template once you map it to your template. This is something that has been improved in Vaadin 18. I doubt this will be backported to Vaadin 14.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文